© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 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 Tutorial 32 – Bookstore Application: Middle Tier form Attributes method/action and Inserting Query Results in a JSP
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Reviewing the Bookstore Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Reviewing the Bookstore Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 add dynamic content (information from a database)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the books.jsp Page (Cont.) Figure 32.4 Closing the result set. Closing the ResultSet
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the books.jsp Page (Cont.) Figure 32.5 Running the updated books.jsp. HTML menu control was filled with book titles by using the HTML option element
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the books.jsp 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the bookInformation.jsp Page (Cont.) Figure Displaying book cover image. Figure Displaying the authors. Display the book cover image Display the book authors
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the bookInformation.jsp Page (Cont.) Figure Displaying the price. Figure Displaying the ISBN. Displaying the book priceDisplaying the book ISBN
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the bookInformation.jsp Page (Cont.) Figure Displaying the edition. Figure Displaying the copyright year. Displaying the book edition Display the book copyright year
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the bookInformation.jsp Page (Cont.) Figure Displaying the description. Figure Closing the ResultSet. Display the book description Closing the ResultSet
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding Functionality to the bookInformation.jsp Page (Cont.) Figure bookInformation.jsp page displaying book information. (Book image courtesy of Prentice Hall.)
2004 Prentice Hall, Inc. All rights reserved. Outline Book List Available Books Select a book from the list and click the button to view 25 the selected book's information books.jsp (1 of 4) Specifying the form ’s action
2004 Prentice Hall, Inc. All rights reserved. Outline <% 32 // setup database connection 33 try 34 { 35 // specify database location 36 System.setProperty( "db2j.system.home", 37 "C:\\Examples\\Tutorial29\\Databases" ); // load Cloudscape driver 40 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" ); // connect to database 43 Connection connection = 44 DriverManager.getConnection( 45 "jdbc:db2j:bookstore" ); 46 books.jsp (2 of 4)
2004 Prentice Hall, Inc. All rights reserved. Outline // obtain list of titles 48 if ( connection != null ) 49 { 50 Statement statement = 51 connection.createStatement(); ResultSet results = statement.executeQuery( 54 "SELECT title FROM Products" ); // display book title 57 while ( results.next() ) 58 { 59 String currentTitle = 60 results.getString( "title" ); %> <% // continue scriptlet } // 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
2004 Prentice Hall, Inc. All rights reserved. Outline results.close(); // close result set // close database connection 74 connection.close(); } // end if } // end try // catch SQLException 81 catch( SQLException exception ) 82 { 83 out.println( 84 "Exception: " + exception + " occurred." ); 85 } %> books.jsp (4 of 4) Closing the ResutSet results
2004 Prentice Hall, Inc. All rights reserved. Outline Book Information bookInformation.jsp (1 of 5) Displaying the book title specified in books.jsp in an h1 header
2004 Prentice Hall, Inc. All rights reserved. Outline <% 25 // setup database connection 26 try 27 { 28 // specify database location 29 System.setProperty( "db2j.system.home", 30 "C:\\Examples\\Tutorial29\\Databases" ); // load Cloudscape driver 33 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" ); // obtain connection to database 36 Connection connection = DriverManager.getConnection( 37 "jdbc:db2j:bookstore" ); // obtain list of titles from database 40 if ( connection != null ) 41 { 42 // create statement 43 Statement statement = connection.createStatement(); 44 bookInformation.jsp (2 of 5)
2004 Prentice Hall, Inc. All rights reserved. Outline // execute query to get book information 46 ResultSet results = statement.executeQuery( 47 "SELECT cover, title, authors, price, isbn, " + 48 "edition, copyrightYear, description " + 49 "FROM Products WHERE title = '" + 50 request.getParameter( "title" ) + "'" ); results.next(); // move cursor to the first row %> <img src = "images/<%= results.getString( 58 "cover" ) %>" alt = "Book cover for 59."> Author(s): <%= results.getString( 63 "authors" ) %> Price: ISBN: 70 bookInformation.jsp (3 of 5) Moving to the first row of the ResultSet Displaying the authorsDisplaying the priceDisplaying the ISBN
2004 Prentice Hall, Inc. All rights reserved. Outline Edition: Copyright Year: <%= results.getString( 76 "copyrightYear" ) %> Description: <%= results.getString( 80 "description" ) %> Book List <% // continue scriptlet results.close(); // close result set 88 connection.close(); // close database connection } // end if } // end try 93 bookInformation.jsp (4 of 5) Displaying the edition number Displaying the copyright year Displaying the description Closing the ResutSet
2004 Prentice Hall, Inc. All rights reserved. Outline // catch SQLException 95 catch( SQLException exception ) 96 { 97 out.println( "Exception: " + exception + " occurred." ); 98 } %> bookInformation.jsp (5 of 5)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Internet and Web Resources java.sun.com/products/jsp/ java.sun.com/products/jsp/docs.html