• No results found

Tutorial 32 –

N/A
N/A
Protected

Academic year: 2021

Share "Tutorial 32 –"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(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

form Attributes method/action and Inserting Query Results in a JSP

(2)

• 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)

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)

(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).

(5)

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

(6)

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)

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)

(8)

Page (Cont.)

Figure 32.4 Closing the result set.

Closing the ResultSet

(9)

HTML menu control was filled with book titles by using the HTML option element

(10)

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)

Clicking this button forwards the user’s request to bookInformation.jsp

(12)

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)

Figure 32.11 Displaying the authors.

Display the book cover image

Display the book authors

(14)

bookInformation.jsp Page (Cont.)

Figure 32.12 Displaying the price.

Figure 32.13 Displaying the ISBN.

Displaying the book price

Displaying the book ISBN

(15)

Figure 32.15 Displaying the copyright year.

Displaying the book edition

Display the book copyright year

(16)

bookInformation.jsp Page (Cont.)

Figure 32.16 Displaying the description.

Figure 32.17 Closing the ResultSet.

Display the book description

Closing the ResultSet

(17)

image courtesy of Prentice Hall.)

(18)

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

(19)

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

(20)

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

(21)

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

(22)

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

(23)

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

(24)

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

(25)

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

(26)

97 out.println( "Exception: " + exception + " occurred." );

98 } 99

100 %> <%-- end scriptlet --%>

101

102 </body>

103 </html>

bookInformation.jsp (5 of 5)

(27)

• java.sun.com/products/jsp/docs.html

• www.jspin.com/home/tutorials

• www.jsp-servlet.net/tomcat_examples.html

References

Related documents

I problematize three family images associated with the design and implementation of housing projects: the bureaucratic family, envisaged by policymakers as conflating with a model

Using information learned from rending about Ancient Greece, have students write about the following prompt:.. &#34;Think about the jobs Ancient Greeks had and how much time

Sensitivity analysis was performed on the lower cut-off voltages to differentiate scar area for three types of catheters (Thermocool, Pentaray, and Orion) with three orientations

I argue that positive global coverage of Jamaica’s outstanding brand achievements in sports, music and as a premier tourism destination, is being negated by its rival brands –

Minors who do not have a valid driver’s license which allows them to operate a motorized vehicle in the state in which they reside will not be permitted to operate a motorized

The NTSB report stated that the high velocity of the diesel in the tank fill piping and the turbulence created in the sump area resulted in the generation of increase static charge

The encryption operation for PBES2 consists of the following steps, which encrypt a message M under a password P to produce a ciphertext C, applying a

CD (%) PTW (%) Number of speed tickets/10 000 habitants Poland Hungary Sweden Estonia Finland Czech Republic Ireland Germany Netherlands Slovenia Greece Belgium Serbia