Outline
32.1 Reviewing the Bookstore Application
32.2 Adding Functionality to the books.jsp Page
32.3 Adding Functionality to the bookInformation.jsp Page 32.4 Internet and Web Resources
32.5 Wrap-Up
form Attributes method/action and Inserting Query Results in a JSP
• 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.
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
(Cont.)
Action Component/Class/Object Event/Method
books.jsp page is requested books.jsp JSP requested by client
Web browser
connect to the database Connection getConnection
create statement Connection createStatement
retrieve the book titles from the database
Statement executeQuery
display them in the select
element ResultSet,
select HTML element, option HTML element
get methods
request the
bookInformation.jsp page input HTML element with type submit
Click the bookInformation.jsp page
is requested bookInformation.jsp JSP requested by client
Web browser
connect to the database Connection getConnection
Figure 30.1 ACE table for the Web-based Bookstore application (Part 1 of 2).
create statement Connection createStatement retrieve the selected book’s
information from the database
Statement executeQuery
format the retrieved information in the bookInformation.jsp
ResultSet,
p (paragraph) HTML elements, img HTML element
get methods
User clicks the Book List link on the bookInformation.jsp page to request the books.jsp page
a (anchor) HTML element User clicks the hyperlink
Figure 30.1 ACE table for the Web-based Bookstore application (Part 2 of 2).
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
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)
Page (Cont.)
Figure 32.4 Closing the result set.
Closing the ResultSet
HTML menu control was filled with book titles by using the HTML option element
Page (Cont.)
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
Clicking this button forwards the user’s request to bookInformation.jsp
bookInformation.jsp Page
Figure 32.8 Displaying the book title.
Displaying book title in an h1 header
Figure 32.9 Accessing the ResultSet results.
Calling method next for the first time positions the
ResultSet cursor in the first row of the ResultSet
Figure 32.11 Displaying the authors.
Display the book cover image
Display the book authors
bookInformation.jsp Page (Cont.)
Figure 32.12 Displaying the price.
Figure 32.13 Displaying the ISBN.
Displaying the book price
Displaying the book ISBN
Figure 32.15 Displaying the copyright year.
Displaying the book edition
Display the book copyright year
bookInformation.jsp Page (Cont.)
Figure 32.16 Displaying the description.
Figure 32.17 Closing the ResultSet.
Display the book description
Closing the ResultSet
image courtesy of Prentice Hall.)
4 <%-- import java.sql.* for database classes --%>
5 <%@ page import = "java.sql.*" %>
6
7 <!-- begin HTML document -->
8 <html>
9
10 <!-- specify HTML head element -->
11 <head>
12
13 <!-- specify page title -->
14 <title>Book List</title>
15 </head>
16
17 <!-- begin body of document -->
18 <body>
19 <h1>Available Books</h1>
20
21 <!-- create form -->
22 <form method = "post" action = "bookInformation.jsp">
23
24 <p>Select a book from the list and click the button to view 25 the selected book's information</p>
books.jsp (1 of 4)
Specifying the
form’s action
33 try 34 {
35 // specify database location
36 System.setProperty( "db2j.system.home", 37 "C:\\Examples\\Tutorial29\\Databases" );
38
39 // load Cloudscape driver
40 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" );
41
42 // connect to database 43 Connection connection =
44 DriverManager.getConnection(
45 "jdbc:db2j:bookstore" );
46
50 Statement statement =
51 connection.createStatement();
52
53 ResultSet results = statement.executeQuery(
54 "SELECT title FROM Products" );
55
56 // display book title 57 while ( results.next() ) 58 { 59 String currentTitle = 60 results.getString( "title" );
61
62 %> <%-- end scriptlet to insert literal HTML and --%>
63 <%-- JSP expressions output from this loop --%>
64
65 <option><%= currentTitle %></option>
66
67 <% // continue scriptlet 68
69 } // end while loop 70
books.jsp (3 of 4)
Retrieving the book title from the ResultSet
Adding the book title to the HTML menu control
78 } // end try 79
80 // catch SQLException
81 catch( SQLException exception ) 82 {
83 out.println(
84 "Exception: " + exception + " occurred." );
85 } 86
87 %> <%-- end scriptlet --%>
88
89 </select>
90
91 <!-- create View Information button -->
92 <p><input type = "submit" value = "View Information"></p>
93 </form>
94
95 </body>
96 </html>
Closing the
ResutSet results
4 <%-- import java.sql.* for database classes --%>
5 <%@ page import = "java.sql.*" %>
6
7 <!-- begin HTML document -->
8 <html>
9
10 <!-- specify head element -->
11 <head>
12
13 <!-- specify page title -->
14 <title>Book Information</title>
15 </head>
16
17 <!-- begin body of document -->
18 <body>
19
20 <!-- create a heading for the book's title -->
21 <h1><%= request.getParameter( "bookTitle" ) %></h1>
22
bookInformation.jsp (1 of 5)
Displaying the book title specified in books.jsp
in an h1 header
30 "C:\\Examples\\Tutorial29\\Databases" );
31
32 // load Cloudscape driver
33 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" );
34
35 // obtain connection to database
36 Connection connection = DriverManager.getConnection(
37 "jdbc:db2j:bookstore" );
38
39 // obtain list of titles from database 40 if ( connection != null )
41 {
42 // create statement
43 Statement statement = connection.createStatement();
44
48 "edition, copyrightYear, description " + 49 "FROM Products WHERE title = '" +
50 request.getParameter( "title" ) + "'" );
51
52 results.next(); // move cursor to the first row 53
54 %> <%-- end scriptlet to insert literal HTML --%>
55
56 <!-- display book cover image -->
57 <img src = "images/<%= results.getString(
58 "cover" ) %>" alt = "Book cover for 59 <%= results.getString( "bookTitle" ) %>.">
60
61 <!-- display authors -->
62 <p>Author(s): <%= results.getString(
63 "authors" ) %></p>
64
65 <!-- display price -->
66 <p>Price: <%= results.getDouble( "price" ) %></p>
67
68 <!-- display ISBN -->
69 <p>ISBN: <%= results.getString( "isbn" ) %></p>
70
bookInformation.jsp (3 of 5)
Moving to the first row of the ResultSet
Displaying the authors
Displaying the price
Displaying the ISBN
78 <!-- display authors -->
79 <p>Description: <%= results.getString(
80 "description" ) %></p>
81
82 <!-- create link to Book List -->
83 <p><a href = "books.jsp">Book List</a></p>
84
85 <% // continue scriptlet 86
87 results.close(); // close result set
88 connection.close(); // close database connection 89
90 } // end if 91
92 } // end try 93
Displaying the edition number
Displaying the copyright year Displaying the description
Closing the ResutSet
97 out.println( "Exception: " + exception + " occurred." );
98 } 99
100 %> <%-- end scriptlet --%>
101
102 </body>
103 </html>
bookInformation.jsp (5 of 5)
• java.sun.com/products/jsp/docs.html
• www.jspin.com/home/tutorials
• www.jsp-servlet.net/tomcat_examples.html