Java Server Pages 2002 Prentice Hall All rights

  • Slides: 34
Download presentation
Java. Server Pages 2002 Prentice Hall. All rights reserved.

Java. Server Pages 2002 Prentice Hall. All rights reserved.

Java. Server Pages Overview • Key components – – Directives Actions Scriptlets Tag libraries

Java. Server Pages Overview • Key components – – Directives Actions Scriptlets Tag libraries 2002 Prentice Hall. All rights reserved.

Java. Server Pages Overview (cont. ) • Directive – Message to JSP container •

Java. Server Pages Overview (cont. ) • Directive – Message to JSP container • i. e. , program that compiles/executes JSPs – Enable programmers to specify • Page settings • Content to include from other resources • Custom tag libraries used in the JSP 2002 Prentice Hall. All rights reserved.

Java. Server Pages Overview (cont. ) • Action – Predefined JSP tags that encapsulate

Java. Server Pages Overview (cont. ) • Action – Predefined JSP tags that encapsulate functionality – Often performed based on information from client request – Can be used to create Java objects for use in scriptlets 2002 Prentice Hall. All rights reserved.

Java. Server Pages Overview (cont. ) • Scriptlet – Also called “Scripting Elements” –

Java. Server Pages Overview (cont. ) • Scriptlet – Also called “Scripting Elements” – Enable programmers to insert Java code in JSPs – Performs request processing • Interacts with page elements and other components to implement dynamic pages 2002 Prentice Hall. All rights reserved.

Java. Server Pages Overview (cont. ) • Custom Tag Library – JSP’s tag extension

Java. Server Pages Overview (cont. ) • Custom Tag Library – JSP’s tag extension mechanism – Enables programmers to define new tags • Tags encapsulate complex functionality – Tags can manipulate JSP content 2002 Prentice Hall. All rights reserved.

Java. Server Pages Overview (cont. ) • JSPs – Look like standard HTML or

Java. Server Pages Overview (cont. ) • JSPs – Look like standard HTML or XHTML • Normally include HTML or XHTML markup – Known as fixed-template data – Used when content is mostly fixed-template data • Small amounts of content generated dynamically • Servlets – Used when small amount of content is fixed-template data • Most content generated dynamically 2002 Prentice Hall. All rights reserved.

A First Java. Server Page Example • Simple JSP example – Demonstrates • Fixed-template

A First Java. Server Page Example • Simple JSP example – Demonstrates • Fixed-template data (XHTML markup) • Creating a Java object (java. util. Date) • Automatic conversion of JSP expression to a String • meta element to refresh Web page at specified interval – First invocation of clock. jsp • Notice the delay while: – JSP container translates the JSP into a servlet – JSP container compiles the servlet – JSP container executes the servlet • Subsequent invocations should not experience the same delay 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 26 27 28 29 30 31 32 33 34 35 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- Fig. 10. 1: clock. jsp --> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <meta http-equiv = "refresh" content = "60" /> <title>A Simple JSP Example</title> <style type = "text/css">. big { font-family: helvetica, arial, sans-serif; font-weight: bold; font-size: 2 em; } </style> </head> Outline Fig. 10. 1 Using a JSP expression to insert the date and time in a Web page (Part 1). Line 10 Line 30 <body> <p class = "big">Simple JSP Example</p> <table style = "border: 6 px outset; "> <tr> <td style = "background-color: black; " > <p class = "big" style = "color: cyan; "> <!-- JSP expression to insert date/time --> <%= new java. util. Date() %> </p> </td> </tr> </table> 2002 Prentice Hall. All rights reserved.

36 37 38 </body> Outline </html> Fig. 10. 1 Using a JSP expression to

36 37 38 </body> Outline </html> Fig. 10. 1 Using a JSP expression to insert the date and time in a Web page (Part 2). Program Output 2002 Prentice Hall. All rights reserved.

Implicit Objects • Implicit Objects – Provide access to many servlet capabilities within a

Implicit Objects • Implicit Objects – Provide access to many servlet capabilities within a JSP – Four scopes • Application scope – Objects owned by the container application – Any servlet or JSP can manipulate these objects • Page scope – Objects that exist only in page in which they are defined – Each page has its own instance of these objects • Request scope – Objects exist for duration of client request – Objects go out of scope when response sent to client • Session scope – Objects exist for duration of client’s browsing session – Objects go out of scope when client terminates session or when session timeout occurs 2002 Prentice Hall. All rights reserved.

Implicit Objects (cont. ) 2002 Prentice Hall. All rights reserved.

Implicit Objects (cont. ) 2002 Prentice Hall. All rights reserved.

Implicit Objects (cont. ) 2002 Prentice Hall. All rights reserved.

Implicit Objects (cont. ) 2002 Prentice Hall. All rights reserved.

Scripting Components • JSP scripting components – – – Scriptlets (delimited by <% and

Scripting Components • JSP scripting components – – – Scriptlets (delimited by <% and %>) Comments (delimited by <%-- and --%>) Expressions (delimited by <%= and %>) Declarations Escape sequences 2002 Prentice Hall. All rights reserved.

Scripting Components (cont. ) 2002 Prentice Hall. All rights reserved.

Scripting Components (cont. ) 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 26 27 28 29 30 31 32 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- Fig. 10. 4: welcome. jsp --> <!-- JSP that processes a "get" request containing data. --> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <!-- head section of document --> <head> <title>Processing "get" requests with data </title> </head> <!-- body section of document --> <body> <% // begin scriptlet Outline Fig. 10. 4 Scripting a Java. Server Page -welcome. jsp (Part 1). Lines 17 -23, 30 -35 Line 19 Line 26 String name = request. get. Parameter( "first. Name" ); if ( name != null ) { %> <%-- end scriptlet to insert fixed template data --%> <h 1> Hello <%= name %>, Welcome to Java. Server Pages! </h 1> <% // continue scriptlet } // end if 2002 Prentice Hall. All rights reserved.

33 34 35 36 37 38 39 40 41 42 43 44 45 46

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 else { Outline %> <%-- end scriptlet to insert fixed template data --%> <form action = "welcome. jsp" method = "get"> <p>Type your first name and press Submit </p> <p><input type = "text" name = "first. Name" /> <input type = "submit" value = "Submit" /> </p> </form> Fig. 10. 4 Scripting a Java. Server Page -welcome. jsp (Part 2). Lines 45 -49 <% // continue scriptlet } // end else %> <%-- end scriptlet --%> </body> </html> <!-- end XHTML document --> 2002 Prentice Hall. All rights reserved.

Outline Fig. 10. 4 Scripting a Java. Server Page -welcome. jsp (Part 3). 2002

Outline Fig. 10. 4 Scripting a Java. Server Page -welcome. jsp (Part 3). 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean> Action 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean> Action 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 26 27 28 29 30 31 Outline // Fig. 10. 20: Guest. Bean. java // Java. Bean to store data for a guest in the guest book. package com. deitel. advjhtp 1. jsp. beans; public class Guest. Bean { private String first. Name, last. Name, email; // set the guest's first name public void set. First. Name( String name ) { first. Name = name; } Fig. 10. 20 Guest. Bean stores information for one guest (Part 1). Line 6 // get the guest's first name public String get. First. Name() { return first. Name; } // set the guest's last name public void set. Last. Name( String name ) { last. Name = name; } // get the guest's last name public String get. Last. Name() { return last. Name; } 2002 Prentice Hall. All rights reserved.

32 33 34 35 36 37 38 39 40 41 42 43 // set

32 33 34 35 36 37 38 39 40 41 42 43 // set the guest's email address public void set. Email( String address ) { email = address; } // get the guest's email address public String get. Email() { return email; } Outline Fig. 10. 20 Guest. Bean stores information for one guest (Part 2). } 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 26 27 28 29 30 31 32 33 34 35 // Fig. 10. 21: Guest. Data. Bean. java // Class Guest. Data. Bean makes a database connection and supports // inserting and retrieving data from the database. package com. deitel. advjhtp 1. jsp. beans; // Java core packages import java. io. *; import java. sql. *; import java. util. *; public class Guest. Data. Bean { private Connection connection; private Prepared. Statement add. Record, get. Records; Outline Fig. 10. 21 Guest. Data. Bean performs database access on behalf of guest. Book. Login. jsp (Part 1). Lines 22 -23 // construct Titles. Bean object public Guest. Data. Bean() throws Exception { // load the Cloudscape driver Class. for. Name( "COM. cloudscape. core. Rmi. Jdbc. Driver" ); // connect to the database connection = Driver. Manager. get. Connection( "jdbc: rmi: jdbc: cloudscape: guestbook" ); get. Records = connection. prepare. Statement( "SELECT first. Name, last. Name, email FROM guests" ); add. Record = connection. prepare. Statement( "INSERT INTO guests ( " + "first. Name, last. Name, email ) " + "VALUES ( ? , ? )" ); 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 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 Outline } // return an Array. List of Guest. Beans public Array. List get. Guest. List() throws SQLException { Array. List guest. List = new Array. List(); // obtain list of titles Result. Set results = get. Records. execute. Query(); // get row data while ( results. next() ) { Guest. Bean guest = new Guest. Bean(); Fig. 10. 21 Guest. Data. Bean performs database access on behalf of guest. Book. Login. jsp (Part 2). Lines 39 -68 guest. set. First. Name( results. get. String( 1 ) ); guest. set. Last. Name( results. get. String( 2 ) ); guest. set. Email( results. get. String( 3 ) ); guest. List. add( guest ); } return guest. List; } // insert a guest in guestbook database public void add. Guest( Guest. Bean guest ) throws SQLException { add. Record. set. String( 1, guest. get. First. Name() ); add. Record. set. String( 2, guest. get. Last. Name() ); add. Record. set. String( 3, guest. get. Email() ); add. Record. execute. Update(); } 2002 Prentice Hall. All rights reserved.

70 71 72 73 74 75 76 77 78 79 80 81 82 83

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 // close statements and terminate database connection protected void finalize() { // attempt to close database connection try { get. Records. close(); add. Record. close(); connection. close(); } // process SQLException on close operation catch ( SQLException sql. Exception ) { sql. Exception. print. Stack. Trace(); } Outline Fig. 10. 21 Guest. Data. Bean performs database access on behalf of guest. Book. Login. jsp (Part 3). } } 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 26 27 28 29 30 31 32 33 34 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- Fig. 10. 22: guest. Book. Login. jsp --> <%-- page settings --%> <%@ page error. Page = "guest. Book. Error. Page. jsp" %> <%-- beans used in this JSP --%> <jsp: use. Bean id = "guest" scope = "page" class = "com. deitel. advjhtp 1. jsp. beans. Guest. Bean" /> <jsp: use. Bean id = "guest. Data" scope = "request" class = "com. deitel. advjhtp 1. jsp. beans. Guest. Data. Bean" /> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <title>Guest Book Login</title> <style type = "text/css"> body { font-family: tahoma, helvetica, arial, sans-serif; } Fig. 10. 22 Java. Server page guest. Book. Login. jsp enables the user to submit a first name, a last name and an e-mail address to be placed in the guest book (Part 1). Line 8 Lines 11 -14 table, tr, td { font-size: . 9 em; border: 3 px groove; padding: 5 px; background-color: #dddddd; } </style> </head> 2002 Prentice Hall. All rights reserved.

35 36 37 38 39 40 41 42 43 44 45 46 47 48

35 36 37 38 39 40 41 42 43 44 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 <body> <jsp: set. Property name = "guest" property = "*" /> <% // start scriptlet if ( guest. get. First. Name() == null || guest. get. Last. Name() == null || guest. get. Email() == null ) { %> <%-- end scriptlet to insert fixed template data --%> <form method = "post" action = "guest. Book. Login. jsp"> <p>Enter your first name, last name and email address to register in our guest book. </p> <table> <tr> <td>First name</td> <input type = "text" name = "first. Name" /> </td> </tr> Outline Fig. 10. 22 Java. Server page guest. Book. Login. jsp enables the user to submit a first name, a last name and an e-mail address to be placed in the guest book (Part 2). Line 36 <tr> <td>Last name</td> <input type = "text" name = "last. Name" /> </td> </tr> <td>Email</td> 2002 Prentice Hall. All rights reserved.

70 71 72 73 74 75 76 77 78 79 80 81 82 83

70 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 97 98 99 100 101 102 <td> <input type = "text" name = "email" /> </td> </tr> <td colspan = "2"> <input type = "submit" value = "Submit" /> </td> </tr> </table> </form> <% // continue scriptlet } // end if else { guest. Data. add. Guest( guest ); Outline Fig. 10. 22 Java. Server page guest. Book. Login. jsp enables the user to submit a first name, a last name and an e-mail address to be placed in the guest book (Part 3). Line 93 %> <%-- end scriptlet to insert jsp: forward action --%> <%-- forward to display guest book contents --%> <jsp: forward page = "guest. Book. View. jsp" /> <% // continue scriptlet } // end else %> <%-- end scriptlet --%> </body> </html> 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 26 27 28 29 30 31 32 33 34 35 <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- Fig. 10. 23: guest. Book. View. jsp --> <%-- page settings --%> <%@ page error. Page = "guest. Book. Error. Page. jsp" %> <%@ page import = "java. util. *" %> <%@ page import = "com. deitel. advjhtp 1. jsp. beans. *" %> <%-- Guest. Data. Bean to obtain guest list --%> <jsp: use. Bean id = "guest. Data" scope = "request" class = "com. deitel. advjhtp 1. jsp. beans. Guest. Data. Bean" /> <html xmlns = "http: //www. w 3. org/1999/xhtml" > Outline Fig. 10. 23 Java. Server page guest. Book. View. jsp displays the contents of the guest book (Part 1). Lines 9 -10 Lines 13 -14 <head> <title>Guest List</title> <style type = "text/css"> body { font-family: tahoma, helvetica, arial, sans-serif; } table, tr, td, th { text-align: center; font-size: . 9 em; border: 3 px groove; padding: 5 px; background-color: #dddddd; } </style> </head> 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 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 <body> <p style = "font-size: 2 em; ">Guest List</p> <table> <thead> <tr> <th style = "width: 100 px; ">Last name</th> <th style = "width: 100 px; ">First name</th> <th style = "width: 200 px; ">Email</th> </tr> </thead> <tbody> Outline Fig. 10. 23 Java. Server page guest. Book. View. jsp displays the contents of the guest book (Part 2). Lines 50 -68 <% // start scriptlet List guest. List = guest. Data. get. Guest. List(); Iterator guest. List. Iterator = guest. List. iterator(); Guest. Bean guest; while ( guest. List. Iterator. has. Next() ) { guest = ( Guest. Bean ) guest. List. Iterator. next(); %> <%-- end scriptlet; insert fixed template data --%> <tr> <td><%= guest. get. Last. Name() %></td> <td><%= guest. get. First. Name() %></td> <a href = "mailto: <%= guest. get. Email() %>"> <%= guest. get. Email() %></a> </td> </tr> 2002 Prentice Hall. All rights reserved.

71 72 73 74 75 76 77 78 79 80 81 82 <% //

71 72 73 74 75 76 77 78 79 80 81 82 <% // continue scriptlet } // end while %> <%-- end scriptlet --%> </tbody> </table> </body> </html> Outline Fig. 10. 23 Java. Server page guest. Book. View. jsp displays the contents of the guest book (Part 3). 2002 Prentice Hall. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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 26 27 28 29 30 31 32 33 34 35 Outline <? xml version = "1. 0"? > <!DOCTYPE html PUBLIC "-//W 3 C//DTD XHTML 1. 0 Strict//EN" "http: //www. w 3. org/TR/xhtml 1/DTD/xhtml 1 -strict. dtd" > <!-- Fig. 10. 24: guest. Book. Error. Page. jsp --> <%-- page settings --%> <%@ page is. Error. Page = "true" %> <%@ page import = "java. util. *" %> <%@ page import = "java. sql. *" %> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <title>Error!</title> <style type = "text/css">. big. Red { font-size: 2 em; color: red; font-weight: bold; } </style> </head> Fig. 10. 24 Java. Server page guest. Book. Error. View. jsp responds to exceptions in guest. Book. Login. jsp and guest. Book. View. jsp (Part 1). Line 8 Line 31 <body> <p class = "big. Red"> <% // scriptlet to determine exception type // and output beginning of error message if ( exception instanceof SQLException ) %> An SQLException 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 <% else if ( exception instanceof Class. Not. Found. Exception ) Outline %> A Class. Not. Found. Exception <% else %> An exception <%-- end scriptlet to insert fixed template data --%> <%-- continue error message output --%> occurred while interacting with the guestbook database. </p> Fig. 10. 24 Java. Server page guest. Book. Error. View. jsp responds to exceptions in guest. Book. Login. jsp and guest. Book. View. jsp (Part 2). Line 37 <p class = "big. Red"> The error message was: <%= exception. get. Message() %> </p> <p class = "big. Red">Please try again later</p> </body> </html> 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean> Action (cont. ) 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean> Action (cont. ) 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean> Action (cont. ) 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean> Action (cont. ) 2002 Prentice Hall. All rights reserved.