Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2."— Presentation transcript:

1 © Copyright 1992-2004 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

2 © Copyright 1992-2004 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.

3 © 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

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 32.1 Reviewing the Bookstore Application (Cont.)

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 32.1 Reviewing the Bookstore Application (Cont.)

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 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

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 add dynamic content (information from a database)

8 © 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 result set. Closing the ResultSet

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 32.2 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

10 © 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.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

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 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

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 Accessing the ResultSet results. Calling method next for the first time positions the ResultSet cursor in the first row of the ResultSet

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 32.3 Adding Functionality to the bookInformation.jsp Page (Cont.) Figure 32.10 Displaying book cover image. Figure 32.11 Displaying the authors. Display the book cover image Display the book authors

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 priceDisplaying the book ISBN

15 © 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. Figure 32.15 Displaying the copyright year. Displaying the book edition Display the book copyright year

16 © 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

17 © 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.)

18  2004 Prentice Hall, Inc. All rights reserved. Outline 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Book List 15 16 17 18 19 Available Books 20 21 22 23 24 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

19  2004 Prentice Hall, Inc. All rights reserved. Outline 19 26 27 28 29 30 31 <% 32 // setup database connection 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 books.jsp (2 of 4)

20  2004 Prentice Hall, Inc. All rights reserved. Outline 20 47 // obtain list of titles 48 if ( connection != null ) 49 { 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 %> 63 64 65 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

21  2004 Prentice Hall, Inc. All rights reserved. Outline 21 71 results.close(); // close result set 72 73 // close database connection 74 connection.close(); 75 76 } // end if 77 78 } // end try 79 80 // catch SQLException 81 catch( SQLException exception ) 82 { 83 out.println( 84 "Exception: " + exception + " occurred." ); 85 } 86 87 %> 88 89 90 91 92 93 94 95 96 books.jsp (4 of 4) Closing the ResutSet results

22  2004 Prentice Hall, Inc. All rights reserved. Outline 22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Book Information 15 16 17 18 19 20 21 22 bookInformation.jsp (1 of 5) Displaying the book title specified in books.jsp in an h1 header

23  2004 Prentice Hall, Inc. All rights reserved. Outline 23 24 <% 25 // setup database connection 26 try 27 { 28 // specify database location 29 System.setProperty( "db2j.system.home", 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 bookInformation.jsp (2 of 5)

24  2004 Prentice Hall, Inc. All rights reserved. Outline 24 45 // 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" ) + "'" ); 51 52 results.next(); // move cursor to the first row 53 54 %> 55 56 57 <img src = "images/<%= results.getString( 58 "cover" ) %>" alt = "Book cover for 59."> 60 61 62 Author(s): <%= results.getString( 63 "authors" ) %> 64 65 66 Price: 67 68 69 ISBN: 70 bookInformation.jsp (3 of 5) Moving to the first row of the ResultSet Displaying the authorsDisplaying the priceDisplaying the ISBN

25  2004 Prentice Hall, Inc. All rights reserved. Outline 25 71 72 Edition: 73 74 75 Copyright Year: <%= results.getString( 76 "copyrightYear" ) %> 77 78 79 Description: <%= results.getString( 80 "description" ) %> 81 82 83 Book List 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 bookInformation.jsp (4 of 5) Displaying the edition number Displaying the copyright year Displaying the description Closing the ResutSet

26  2004 Prentice Hall, Inc. All rights reserved. Outline 26 94 // catch SQLException 95 catch( SQLException exception ) 96 { 97 out.println( "Exception: " + exception + " occurred." ); 98 } 99 100 %> 101 102 103 bookInformation.jsp (5 of 5)

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 32.4Internet 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


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2."

Similar presentations


Ads by Google