1 Chapter 25 Java Server Pages Outline 25

  • Slides: 44
Download presentation
1 Chapter 25: Java. Server Pages Outline 25. 1 25. 2 25. 3 25.

1 Chapter 25: Java. Server Pages Outline 25. 1 25. 2 25. 3 25. 4 25. 5 25. 6 Introduction Java. Server Pages Overview First Java. Server Page Example Implicit Objects Scripting 25. 5. 1 Scripting Components 25. 5. 2 Scripting Example Standard Actions 25. 6. 1 <jsp: include> Action 25. 6. 2 <jsp: forward> Action 25. 6. 3 <jsp: use. Bean> Action 2003 Prentice Hall, Inc. All rights reserved.

2 25. 1 Introduction • Java. Server Pages – Extension of Servlet technology •

2 25. 1 Introduction • Java. Server Pages – Extension of Servlet technology • Simplify delivery of dynamic Web content • Reuse existing Java components – Without programming Java – Use scripts embedded in HTML files • Classes and interfaces specific to JSP – Package javax. servlet. jsp. tagext 2003 Prentice Hall, Inc. All rights reserved.

3 25. 2 Java. Server Pages Overview • Key components – – Directives Actions

3 25. 2 Java. Server Pages Overview • Key components – – Directives Actions Scriptlets Tag libraries 2003 Prentice Hall, Inc. All rights reserved.

4 25. 2 Java. Server Pages Overview (cont. ) • Directive – Message to

4 25. 2 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 2003 Prentice Hall, Inc. All rights reserved.

5 25. 2 Java. Server Pages Overview (cont. ) • Action – – Predefined

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

6 25. 2 Java. Server Pages Overview (cont. ) • Scriptlet – Also called

6 25. 2 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 2003 Prentice Hall, Inc. All rights reserved.

7 25. 2 Java. Server Pages Overview (cont. ) • Custom Tag Library –

7 25. 2 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 2003 Prentice Hall, Inc. All rights reserved.

8 25. 2 Java. Server Pages Overview (cont. ) • JSPs – Look like

8 25. 2 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 2003 Prentice Hall, Inc. All rights reserved.

9 25. 2 Java. Server Pages Overview (cont. ) • Some servlets do not

9 25. 2 Java. Server Pages Overview (cont. ) • Some servlets do not produce content – Invoke other servlets and JSPs • JSPs execute as part of a Web server – JSP container • JSP first request – JSP container translates a JSP into a servlet • Handle the current and future requests • Code that represents the JSP – Placed in servlet’s _jsp. Service method 2003 Prentice Hall, Inc. All rights reserved.

10 25. 2 Java. Server Pages Overview (cont. ) • JSP errors – Translation-time

10 25. 2 Java. Server Pages Overview (cont. ) • JSP errors – Translation-time errors • Occur when JSPs are translated into servlets – Request-time errors • Occur during request processing • Methods jsp. Init and jsp. Destroy – Container invokes when initializing and terminating a JSP • Methods are defined in JSP declarations – Part of the JSP scripting mechanism 2003 Prentice Hall, Inc. All rights reserved.

11 25. 3 A First Java. Server Page Example • Simple JSP example (Fig.

11 25. 3 A First Java. Server Page Example • Simple JSP example (Fig. 25. 1) – 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 • Some 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 2003 Prentice Hall, Inc. All rights reserved.

12 25. 3 A First Java. Server Page Example • Simple JSP example (Fig.

12 25. 3 A First Java. Server Page Example • Simple JSP example (Fig. 25. 1) <%= new java. util. Date() %> • JSP expression, String representation inserted into response to client 2003 Prentice Hall, Inc. 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 <? 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" > Outline 13 <!-- Fig. 25. 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> clock. jsp meta element refreshes the 10 Web page every. Line 60 seconds Meta element refershes the Web page every 60 seconds. <body> <p class = "big">Simple JSP Example</p> 2003 Prentice Hall, Inc. All rights reserved.

24 25 26 27 28 29 30 31 32 33 34 35 36 37

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 <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> </body> Outline Creates Date object that is converted to a String implicitly and displayed in paragraph (p) element clock. jsp </html> Line 30 Creates Date object 2003 Prentice Hall, Inc. All rights reserved. 14

Outline Clock. jsp 2003 Prentice Hall, Inc. All rights reserved. 15

Outline Clock. jsp 2003 Prentice Hall, Inc. All rights reserved. 15

16 25. 4 Implicit Objects • Implicit Objects – Provide access to many servlet

16 25. 4 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 after 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 2003 Prentice Hall, Inc. All rights reserved.

17 2003 Prentice Hall, Inc. All rights reserved.

17 2003 Prentice Hall, Inc. All rights reserved.

18 25. 5 Scripting • Scripting – Dynamically generated content – Insert Java code

18 25. 5 Scripting • Scripting – Dynamically generated content – Insert Java code and logic in JSP using scripting 2003 Prentice Hall, Inc. All rights reserved.

19 25. 5. 1 Scripting Components • JSP scripting components – Scriptlets (blocks of

19 25. 5. 1 Scripting Components • JSP scripting components – Scriptlets (blocks of code containing Java statements delimited by <% and %>) – Comments • JSP comments (delimited by <%-- and --%>) • XHTML comments (delimited by <!-- and -->) • Java’s comments (delimited by // and /* and */) – Expressions (delimited by <%= and %>) (evaluated when client requests the JSP, String version sent in response) – Declarations (delimited by <%! and %>) (define variables for use in JSP • <%! int counter = 0; %> 2003 Prentice Hall, Inc. All rights reserved.

20 25. 5. 2 Scripting Example • Demonstrate basic scripting capabilities – Responding to

20 25. 5. 2 Scripting Example • Demonstrate basic scripting capabilities – Responding to get requests 2003 Prentice Hall, Inc. 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 <? 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" > Outline <!-- Fig. 25. 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 welcome. jsp Lines 17 -23 Scriptlets used to insert Java code Line 19 String name = request. get. Parameter( "first. Name" ); if ( name != null ) { Use request implicit object to get parameter %> <%-- end scriptlet to insert fixed template data --%> 2003 Prentice Hall, Inc. All rights reserved. 21

25 26 27 28 29 30 31 32 33 34 35 36 37 38

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 <h 1> Hello <%= name %>, Welcome to Java. Server Pages! </h 1> JSP expression <% // continue scriptlet Outline 22 Scriptlets used to insert Java code } // end if else { %> <%-- 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> welcome. jsp Line 26 Lines 30 -35 and 45 -49 <% // continue scriptlet } // end else %> <%-- end scriptlet --%> </body> </html> <!-- end XHTML document --> 2003 Prentice Hall, Inc. All rights reserved.

Outline welcome. jsp 2003 Prentice Hall, Inc. All rights reserved. 23

Outline welcome. jsp 2003 Prentice Hall, Inc. All rights reserved. 23

24 25. 6 Standard Actions • JSP standard actions (most common tasks) – Provide

24 25. 6 Standard Actions • JSP standard actions (most common tasks) – Provide access to common tasks performed in a JSP • Including content from other resources • Forwarding requests to other resources • Interacting with Java. Beans – JSP containers process actions at request time – Delimited by <jsp: action> and </jsp: action> 2003 Prentice Hall, Inc. All rights reserved.

25 25. 6 Standard Actions 2003 Prentice Hall, Inc. All rights reserved.

25 25. 6 Standard Actions 2003 Prentice Hall, Inc. All rights reserved.

26 25. 6. 1 <jsp: include> Action • <jsp: include> action – Enables dynamic

26 25. 6. 1 <jsp: include> Action • <jsp: include> action – Enables dynamic content to be included in a JSP at request time – More flexible than include directive (included at translation time) • Requires more overhead when page contents change frequently • <jsp: include page = "toc. html" flush = "true" /> – page is the resource to include – flush must be true to say to flush buffer after including 2003 Prentice Hall, Inc. 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 <!-- Fig. 25. 7: banner. html --> <!-- banner to include in another document --> <div style = "width: 580 px"> <p> Java(TM), C, C++, Visual Basic(R), Object Technology, and Internet and World Wide Web Programming Training  On-Site Seminars Delivered Worldwide </p> Outline <p> <a href = "mailto: deitel@deitel. com">deitel@deitel. com</a> 978. 461. 5880 12 Clock Tower Place, Suite 200, Maynard, MA 01754 </p> </div> banner. html 2003 Prentice Hall, Inc. All rights reserved. 27

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 <!-- Fig. 25. 8: toc. html --> <!-- contents to include in another document --> Outline <p><a href = "http: //www. deitel. com/books/index. html"> Publications/Book. Store </a></p> <p><a href = "http: //www. deitel. com/whatsnew. html"> What's New </a></p> <p><a href = "http: //www. deitel. com/books/downloads. html"> Downloads/Resources </a></p> <p><a href = "http: //www. deitel. com/faq/index. html"> FAQ (Frequently Asked Questions) </a></p> toc. html <p><a href = "http: //www. deitel. com/intro. html"> Who we are </a></p> 2003 Prentice Hall, Inc. All rights reserved. 28

24 25 26 27 28 29 30 31 32 33 34 <p><a href =

24 25 26 27 28 29 30 31 32 33 34 <p><a href = "http: //www. deitel. com/index. html"> Home Page </a></p> Outline <p>Send questions or comments about this site to <a href = "mailto: deitel@deitel. com"> deitel@deitel. com </a> Copyright 1995 -2003 by Deitel & Associates, Inc. All Rights Reserved. </p> toc. html 2003 Prentice Hall, Inc. All rights reserved. 29

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 <!-- Fig. 25. 9: clock 2. jsp --> <!-- date and time to include in another document --> Outline <table> <tr> <td style = "background-color: black; "> <p class = "big" style = "color: cyan; font-size: 3 em; font-weight: bold; "> <%-- script to determine client local and --%> <%-- format date accordingly --%> <% // get client locale java. util. Locale locale = request. get. Locale(); // get Date. Format for client's Locale java. text. Date. Format date. Format = java. text. Date. Format. get. Date. Time. Instance( java. text. Date. Format. LONG, locale ); %> Use Locale to format Date with specified clock 2. jsp Date. Format Lines 14 -20 <%-- end script --%> <%-- output date --%> <%= date. Format. format( new java. util. Date() ) %> </p> </td> </tr> </table> 2003 Prentice Hall, Inc. All rights reserved. 30

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 <? 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" > Outline <!-- Fig. 25. 10: include. jsp --> <html xmlns = "http: //www. w 3. org/1999/xhtml"> <head> <title>Using jsp: include</title> <style type = "text/css"> body { font-family: tahoma, helvetica, arial, sans-serif; } include. jsp table, tr, td { font-size: . 9 em; border: 3 px groove; padding: 5 px; background-color: #dddddd; } </style> </head> 2003 Prentice Hall, Inc. All rights reserved. 31

26 27 28 29 30 31 32 33 34 35 36 37 38 39

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <body> <table> <tr> <td style = <img src width alt = </td> Outline "width: 160 px; text-align: center"> = "images/logotiny. png" = "140" height = "93" "Deitel & Associates, Inc. Logo" /> <td> <%-- include banner. html in this JSP --%> <jsp: include page = "banner. html" flush = "true" /> </td> </tr> <td style = "width: 160 px"> include. jsp Use JSPLines action 38 -39 to include banner. html Use JSP action to include banner. html. Lineaction 48 to Use JSP action to include toc. html. <%-- include toc. html in this JSP --%> <jsp: include page = "toc. html" flush = "true" /> </td> 2003 Prentice Hall, Inc. All rights reserved. 32

52 53 54 55 56 57 58 59 60 61 62 <td style =

52 53 54 55 56 57 58 59 60 61 62 <td style = "vertical-align: top"> <%-- include clock 2. jsp in this JSP --%> <jsp: include page = "clock 2. jsp" flush = "true" /> Outline Use JSP action to include clock 2. jsp </td> </tr> </table> </body> </html> include. jsp Lines 55 -56 Use JSP action to include clock 2. jsp. 2003 Prentice Hall, Inc. All rights reserved. 33

Outline include. jsp 2003 Prentice Hall, Inc. All rights reserved. 34

Outline include. jsp 2003 Prentice Hall, Inc. All rights reserved. 34

35 25. 6. 2 <jsp: forward> Action • <jsp: forward> action – Enables JSP

35 25. 6. 2 <jsp: forward> Action • <jsp: forward> action – Enables JSP to forward request to different resources • Can forward requests only to resources in same context • Original JSP terminates • <jsp: param> action – Specifies name/value pairs of information • Name/Value pairs are passed to other actions 2003 Prentice Hall, Inc. 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 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. 25. 11: forward 1. jsp --> <html xmlns = "http: //www. w 3. org/1999/xhtml"> <head> <title>Forward request to another JSP</title> </head> <body> <% // begin scriptlet String name = request. get. Parameter( "first. Name" ); forward 1. jsp Lines 22 -25 if ( name != null ) { %> <%-- end scriptlet to insert fixed template data --%> <jsp: forward page = "forward 2. jsp"> <jsp: param name = "date" value = "<%= new java. util. Date() %>" /> </jsp: forward> Forward request to forward 2. jsp 2003 Prentice Hall, Inc. All rights reserved. 36

27 28 29 30 31 32 33 34 35 36 37 38 39 40

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 <% // continue scriptlet Outline } // end if else { %> <%-- end scriptlet to insert fixed template data --%> <form action = "forward 1. 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> <% // continue scriptlet } forward 1. jsp // end else %> <%-- end scriptlet --%> </body> </html> <!-- end XHTML document --> 2003 Prentice Hall, Inc. All rights reserved. 37

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 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" > <!-- forward 2. jsp --> <html xmlns = "http: //www. w 3. org/1999/xhtml"v <head> <title>Processing a forwarded request</title> <style type = "text/css">. big { font-family: tahoma, helvetica, arial, sans-serif; font-weight: bold; font-size: 2 em; } </style> </head> forward 2. jsp Lines 23 -24 Receive request from forward 1. jsp, then get first. Name parameter from request <body> <p class = "big"> Hello <%= request. get. Parameter( "first. Name" ) %>, Your request was received and forwarded at </p> 2003 Prentice Hall, Inc. All rights reserved. 38

27 28 29 30 31 32 33 34 35 36 37 38 <table style

27 28 29 30 31 32 33 34 35 36 37 38 <table style = "border: 6 px outset; "> <tr> <td style = "background-color: black; "> <p class = "big" style = "color: cyan; "> <%= request. get. Parameter( "date" ) %> </p> </td> </tr> </table> </body> Outline Get date parameter from request </html> forward 2. jsp Line 31 2003 Prentice Hall, Inc. All rights reserved. 39

40 25. 6. 3 <jsp: use. Bean> Action • <jsp: use. Bean> action –

40 25. 6. 3 <jsp: use. Bean> Action • <jsp: use. Bean> action – Enables JSP to manipulate Java object • Creates Java object or locates an existing object for use in JSP – <jsp: get. Property name = "rotator" property = "link“ /> same as – <%= rotator. get. Link() %> 2003 Prentice Hall, Inc. 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 Outline // Fig. 25. 14: Rotator. java // A Java. Bean that rotates advertisements. package com. deitel. jhtp 5. jsp; public class Rotator { private String images[] = { "images/advj. HTP 1. jpg", "images/cpp. HTP 4. jpg", "images/iw 3 HTP 2. jpg", "images/jws. FEP 1. jpg", "images/vbnet. HTP 2. jpg" }; private String links[] = { "http: //www. amazon. com/exec/obidos/ASIN/0130895601/" "deitelassociatin", "http: //www. amazon. com/exec/obidos/ASIN/0130384747/" "deitelassociatin", "http: //www. amazon. com/exec/obidos/ASIN/0130308978/" "deitelassociatin", "http: //www. amazon. com/exec/obidos/ASIN/0130461342/" "deitelassociatin", "http: //www. amazon. com/exec/obidos/ASIN/0130293636/" "deitelassociatin" }; + + + Rotator. java + + private int selected. Index = 0; 2003 Prentice Hall, Inc. All rights reserved. 41

24 25 26 27 28 29 30 31 32 33 34 35 36 37

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 Return image file name for book cover image // returns the URL for ad's corresponding Web site public String get. Link() { return links[ selected. Index ]; } Return hyperlink to book at Amazon. com // update selected. Index so next calls to get. Image and // get. Link return a different advertisement public void next. Ad() { selected. Index = ( selected. Index + 1 ) % images. length; } } Outline // returns image file name for current ad public String get. Image() { return images[ selected. Index ]; } Rotator. java Update Rotator so subsequent calls to Lines 25 -28 get. Image and get. Link return Lines information 31 -34 for different advertisements Lines 38 -41 2003 Prentice Hall, Inc. All rights reserved. 42

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 <? 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" > Outline <!-- Fig. 25. 15: adrotator. jsp --> <jsp: use. Bean id = "rotator" scope = "application" class = "com. deitel. jhtp 5. jsp. Rotator" /> Use jsp: use. Bean action to obtain reference to Rotator object <html xmlns = "http: //www. w 3. org/1999/xhtml"> <head> <title>Ad. Rotator Example</title> <style type = "text/css">. big { font-family: helvetica, arial, sans-serif; font-weight: bold; font-size: 2 em } </style> <%-- update advertisement --%> <% rotator. next. Ad(); %> </head> adrotator. jsp Lines 7 -8 Line 22 Invoke Rotator’s next. Ad method 2003 Prentice Hall, Inc. All rights reserved. 43

25 26 27 28 29 30 31 32 33 34 35 36 37 <body>

25 26 27 28 29 30 31 32 33 34 35 36 37 <body> <p class = "big">Ad. Rotator Example</p> <a href = "<jsp: get. Property name = "rotator" property = "link" />"> Outline Define hyperlink to Amazon. com site <img src = "<jsp: get. Property name = "rotator" property = "image" />" alt = "advertisement" /> </a> </p> </body> </html> adrotator. jsp Lines 29 -33 2003 Prentice Hall, Inc. All rights reserved. 44