Java Server Pages 2002 Prentice Hall All rights

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

Java. Server Pages 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. ) • 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. ) • 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 (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 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.