simplyjava1_32

Download Report

Transcript simplyjava1_32

Tutorial 32 – Bookstore Application:
Middle Tier
form Attributes method/action and Inserting
Query Results in a JSP
Outline
32.1
32.2
32.3
32.4
32.5
Reviewing the Bookstore Application
Adding Functionality to the books.jsp Page
Adding Functionality to the bookInformation.jsp Page
Internet and Web Resources
Wrap-Up
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
1
2
Objectives
• In this tutorial, you will learn to:
– Write the functionality for the middle tier, using JSP.
– Process a ResultSet inside a JSP scriptlet.
– Use JSP expressions to insert content in a JSP.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
3
32.1
Reviewing the Bookstore Application
When the books.jsp page is requested
Retrieve the book titles from the database
Display the book titles in an HTML menu
If the user selects a book title from the menu and clicks the View Information (submit) button
Request the bookInformation.jsp page for the selected title
When the bookInformation.jsp page is requested from books.jsp
Retrieve the selected book’s information from a database for the selected title
Format the retrieved information in the bookInformation.jsp page
Return the result to the client browser
If the user clicks the Book List link on the bookInformation.jsp page
Request the books.jsp page
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
4
32.1
Reviewing the Bookstore Application
(Cont.)
Action
books.jsp page is requested
connect to the database
Connection
Event/Method
JSP requested by client
Web browser
getConnection
create statement
Connection
createStatement
retrieve the book titles from
the database
display them in the select
element
Statement
executeQuery
ResultSet,
select HTML element,
option HTML element
input HTML element with type
submit
bookInformation.jsp
get methods
Connection
getConnection
request the
bookInformation.jsp page
the bookInformation.jsp page
is requested
connect to the database
Component/Class/Object
books.jsp
Click
JSP requested by client
Web browser
Figure 30.1 ACE table for the Web-based Bookstore application (Part 1 of 2).
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
5
32.1
Action
create statement
Reviewing the Bookstore Application
(Cont.)
Component/Class/Object
Connection
Event/Method
createStatement
Statement
executeQuery
retrieve the selected book’s
information from the
database
format the retrieved
ResultSet,
get methods
information in the
p (paragraph) HTML elements, img
bookInformation.jsp
HTML element
User clicks the Book List link a (anchor) HTML element
User clicks the hyperlink
on the bookInformation.jsp
page to request the books.jsp
page
Figure 30.1 ACE table for the Web-based Bookstore application (Part 2 of 2).
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
32.2
Adding Functionality to the books.jsp
Page
Figure 32.2
while statement that gets book titles from the ResultSet.
while statement
that iterates through
the ResultSet and
gets each book title
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
6
7
32.2
Adding Functionality to the books.jsp
Page (Cont.)
Figure 32.3
Displaying the current book title.
Adding book titles to
HTML menu control
• option element
• Adds items to the menu control
• JSP expression
• <%= code %>
• add dynamic content (information from a database)
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
8
32.2
Adding Functionality to the books.jsp
Page (Cont.)
Figure 32.4
Closing the ResultSet
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Closing the result set.
32.2
Adding Functionality to the books.jsp
Page (Cont.)
Figure 32.5
HTML menu control was filled
with book titles by using the
HTML option element
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Running the updated books.jsp.
9
Adding Functionality to the books.jsp
Page (Cont.)
32.2
Figure 32.6
Adding action to the form element.
Specifying
form action
• form element
– method attribute
• Specifies how data is sent to the Web server
– action attribute
• Specifies task to perform when user submits the form
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
10
32.2
Adding Functionality to the books.jsp
Page (Cont.)
Figure 32.7
Running the updated books.jsp page.
Clicking this button
forwards the user’s request
to bookInformation.jsp
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
11
12
32.3 Adding Functionality to the
bookInformation.jsp Page
Figure 32.8
Displaying the book title.
Displaying book title
in an h1 header
Figure 32.9
Calling method next for
the first time positions the
ResultSet cursor in the
first row of the ResultSet
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Accessing the ResultSet results.
13
32.3 Adding Functionality to the
bookInformation.jsp Page (Cont.)
Figure 32.10
Displaying book cover image.
Display the book
cover image
Figure 32.11
Display the book
authors
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Displaying the authors.
14
32.3 Adding Functionality to the
bookInformation.jsp Page (Cont.)
Figure 32.12
Displaying the price.
Figure 32.13
Displaying the ISBN.
Displaying the book price
Displaying the book ISBN
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
15
32.3 Adding Functionality to the
bookInformation.jsp Page (Cont.)
Figure 32.14
Displaying the edition.
Displaying the
book edition
Figure 32.15 Displaying the copyright year.
Display the book
copyright year
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
16
32.3 Adding Functionality to the
bookInformation.jsp Page (Cont.)
Figure 32.16
Displaying the description.
Figure 32.17
Closing the ResultSet.
Display the book
description
Closing the ResultSet
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
17
32.3 Adding Functionality to the
bookInformation.jsp Page (Cont.)
Figure 32.18 bookInformation.jsp page displaying book information. (Book
image courtesy of Prentice Hall.)
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!-- Tutorial 32: books.jsp -->
<!-- Displays a form.
-->
<%-- import java.sql.* for database classes --%>
<%@ page import = "java.sql.*" %>
18
Outline
books.jsp
(1 of 4)
<!-- begin HTML document -->
<html>
<!-- specify HTML head element -->
<head>
<!-- specify page title -->
<title>Book List</title>
</head>
<!-- begin body of document -->
<body>
<h1>Available Books</h1>
<!-- create form -->
<form method = "post" action = "bookInformation.jsp">
Specifying the
form’s action
<p>Select a book from the list and click the button to view
the selected book's information</p>
 2004 Prentice Hall, Inc.
All rights reserved.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
19
<!-- create list that contains book titles -->
<select name = "bookTitle">
<%-- begin JSP scriptlet to connect to database --%>
<%
// setup database connection
try
{
// specify database location
System.setProperty( "db2j.system.home",
"C:\\Examples\\Tutorial29\\Databases" );
Outline
books.jsp
(2 of 4)
// load Cloudscape driver
Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" );
// connect to database
Connection connection =
DriverManager.getConnection(
"jdbc:db2j:bookstore" );
 2004 Prentice Hall, Inc.
All rights reserved.
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// obtain list of titles
if ( connection != null )
{
Statement statement =
connection.createStatement();
20
Outline
books.jsp
(3 of 4)
ResultSet results = statement.executeQuery(
"SELECT title FROM Products" );
// display book title
while ( results.next() )
{
String currentTitle =
results.getString( "title" );
%>
<%-- end scriptlet to insert literal HTML and --%>
<%-- JSP expressions output from this loop
--%>
<option><%= currentTitle %></option>
<%
Retrieving the book title
from the ResultSet
Adding the book title to the
HTML menu control
// continue scriptlet
}
// end while loop
 2004 Prentice Hall, Inc.
All rights reserved.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
results.close();
// close result set
// close database connection
connection.close();
}
// end if
21
Outline
books.jsp
(4 of 4)
Closing the
}
// end try
ResutSet results
// catch SQLException
catch( SQLException exception )
{
out.println(
"Exception: " + exception + " occurred." );
}
%>
<%-- end scriptlet --%>
</select>
<!-- create View Information button -->
<p><input type = "submit" value = "View Information"></p>
</form>
</body>
</html>
 2004 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- Tutorial 32: bookInformation.jsp -->
<!-- Displays book information.
-->
<%-- import java.sql.* for database classes --%>
<%@ page import = "java.sql.*" %>
22
Outline
bookInformation.jsp
(1 of 5)
<!-- begin HTML document -->
<html>
<!-- specify head element -->
<head>
<!-- specify page title -->
<title>Book Information</title>
</head>
<!-- begin body of document -->
<body>
<!-- create a heading for the book's title -->
<h1><%= request.getParameter( "bookTitle" ) %></h1>
Displaying the book title
specified in books.jsp
in an h1 header
 2004 Prentice Hall, Inc.
All rights reserved.
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<%-- begin JSP scriptlet to connect to a database --%>
<%
// setup database connection
try
{
// specify database location
System.setProperty( "db2j.system.home",
"C:\\Examples\\Tutorial29\\Databases" );
23
Outline
bookInformation.jsp
(2 of 5)
// load Cloudscape driver
Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" );
// obtain connection to database
Connection connection = DriverManager.getConnection(
"jdbc:db2j:bookstore" );
// obtain list of titles from database
if ( connection != null )
{
// create statement
Statement statement = connection.createStatement();
 2004 Prentice Hall, Inc.
All rights reserved.
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// execute query to get book information
ResultSet results = statement.executeQuery(
"SELECT cover, title, authors, price, isbn, " +
"edition, copyrightYear, description " +
"FROM Products WHERE title = '" +
request.getParameter( "title" ) + "'" );
results.next();
%>
// move cursor to the first row
24
Outline
bookInformation.jsp
(3 of 5)
Moving to the first
row of the ResultSet
<%-- end scriptlet to insert literal HTML --%>
<!-- display book cover image -->
<img src = "images/<%= results.getString(
"cover" ) %>" alt = "Book cover for
<%= results.getString( "bookTitle" ) %>.">
<!-- display authors -->
<p>Author(s): <%= results.getString(
"authors" ) %></p>
<!-- display price -->
<p>Price: <%= results.getDouble( "price" ) %></p>
<!-- display ISBN -->
<p>ISBN: <%= results.getString( "isbn" ) %></p>
Displaying the authors
Displaying the price
Displaying the ISBN
 2004 Prentice Hall, Inc.
All rights reserved.
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!-- display edition number -->
<p>Edition: <%= results.getInt( "edition" ) %></p>
<!-- display copyright year -->
<p>Copyright Year: <%= results.getString(
"copyrightYear" ) %></p>
<!-- display authors -->
<p>Description: <%= results.getString(
"description" ) %></p>
<!-- create link to Book List -->
<p><a href = "books.jsp">Book List</a></p>
<%
// continue scriptlet
results.close(); // close result set
connection.close(); // close database connection
}
}
25
Outline
bookInformation.jsp
(4 of 5)
Displaying the edition
number
Displaying the
copyright year
Displaying the
description
Closing the ResutSet
// end if
// end try
 2004 Prentice Hall, Inc.
All rights reserved.
94
// catch SQLException
95
catch( SQLException exception )
96
{
97
out.println( "Exception: " + exception + " occurred." );
98
}
99
100
%> <%-- end scriptlet --%>
101
102
</body>
103 </html>
26
Outline
bookInformation.jsp
(5 of 5)
 2004 Prentice Hall, Inc.
All rights reserved.
27
32.4 Internet and Web Resources
•
•
•
•
java.sun.com/products/jsp/
java.sun.com/products/jsp/docs.html
www.jspin.com/home/tutorials
www.jsp-servlet.net/tomcat_examples.html
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.