Chapter 10 Java Server Pages Outline 10 1

  • Slides: 100
Download presentation
Chapter 10: Java. Server Pages Outline 10. 1 10. 2 10. 3 10. 4

Chapter 10: Java. Server Pages Outline 10. 1 10. 2 10. 3 10. 4 10. 5 10. 6 10. 7 Introduction Java. Server Pages Overview A First Java. Server Page Example Implicit Objects Scripting 10. 5. 1 Scripting Components 10. 5. 2 Scripting Example Standard Actions 10. 6. 1 <jsp: include> Action 10. 6. 2 <jsp: forward> Action 10. 6. 3 <jsp: plugin> Action 10. 6. 4 <jsp: use. Bean> Action Directives 10. 7. 1 page Directive 10. 7. 2 include Directive 2002 Prentice Hall. All rights reserved.

Chapter 10: Java. Server Pages 10. 8 Custom Tag Libraries 10. 8. 1 Simple

Chapter 10: Java. Server Pages 10. 8 Custom Tag Libraries 10. 8. 1 Simple Custom Tag 10. 8. 2 Custom Tag with Attributes 10. 8. 3 Evaluating the Body of a Custom Tag 2002 Prentice Hall. All rights reserved.

10. 1 Introduction • Java. Server Pages – Extension of Servlet technology • Web

10. 1 Introduction • Java. Server Pages – Extension of Servlet technology • Web content delivery • Reuse existing Java components – Without programming Java • Create custom tags – Encapsulate complex functionality • Classes and interfaces specific to JSP – Package javax. servlet. jsp. tagext 2002 Prentice Hall. All rights reserved.

10. 2 Java. Server Pages Overview • Key components – – Directives Actions Scriptlets

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

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

10. 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 • Custom tag libraries used in the JSP 2002 Prentice Hall. All rights reserved.

10. 2 Java. Server Pages Overview (cont. ) • Action – Predefined JSP tags

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

10. 2 Java. Server Pages Overview (cont. ) • Scriptlet – Also called “Scripting

10. 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 2002 Prentice Hall. All rights reserved.

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

10. 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 2002 Prentice Hall. All rights reserved.

10. 2 Java. Server Pages Overview (cont. ) • JSPs – Look like standard

10. 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 2002 Prentice Hall. All rights reserved.

10. 2 Java. Server Pages Overview (cont. ) • Some servlets do not produce

10. 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 2002 Prentice Hall. All rights reserved.

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

10. 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 2002 Prentice Hall. All rights reserved.

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

10. 3 A First Java. Server Page Example • Simple JSP example (Fig. 10. 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 • 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" > Outline <!-- 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> <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> Fig. 10. 1 Using meta element refreshes the a expression Web page. JSP every 60 secondsto insert the date and time in a Web page (Part 1). Line 10 Line 30 Creates Date object that is converted to a String implicitly and displayed in paragraph (p) element 2002 Prentice Hall. All rights reserved.

36 37 38 </body> Outline </html> Fig. 10. 1 Usingofa String representation JSP expression

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

10. 4 Implicit Objects • Implicit Objects – Provide access to many servlet capabilities

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

10. 4 Implicit Objects (cont. ) • JSP implicit objects – Extend classes or

10. 4 Implicit Objects (cont. ) • JSP implicit objects – Extend classes or implement interfaces • Discussed in Chapter 9 – Such objects can invoke public aspects of classes/interfaces 2002 Prentice Hall. All rights reserved.

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

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

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

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

10. 5 Scripting • Scripting – How JSP programmers can insert Java code and

10. 5 Scripting • Scripting – How JSP programmers can insert Java code and logic – Currently, JSP support scripting only with Java 2002 Prentice Hall. All rights reserved.

10. 5. 1 Scripting Components • JSP scripting components – – – Scriptlets (delimited

10. 5. 1 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.

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

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

10. 5. 2 Scripting Example • Demonstrate basic scripting capabilities – Responding to get

10. 5. 2 Scripting Example • Demonstrate basic scripting capabilities – Responding to get requests 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" > Outline <!-- 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 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> Fig. 10. 4 Scripting a Java. Server Page -welcome. jsp Scriptlets used to (Part 1). Java code insert 17 -23, implicit 30 -35 Use. Lines request object to get parameter Line 19 JSP declaration Line 26 <% // 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> <% // continue scriptlet } // end else %> <%-- end scriptlet --%> </body> </html> <!-- end XHTML document --> Scriptlets used to insert Java code Fig. 10. 4 Scripting a Java. Server Page -welcome. jsp (Part 2). Lines 45 -49 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.

10. 6 Standard Actions • JSP standard actions – Provide access to common tasks

10. 6 Standard Actions • JSP standard actions – 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> 2002 Prentice Hall. All rights reserved.

10. 6 Standard Actions 2002 Prentice Hall. All rights reserved.

10. 6 Standard Actions 2002 Prentice Hall. All rights reserved.

10. 6 Standard Actions (cont. ) 2002 Prentice Hall. All rights reserved.

10. 6 Standard Actions (cont. ) 2002 Prentice Hall. All rights reserved.

10. 6. 1 <jsp: include> Action • <jsp: include> action – Enables dynamic content

10. 6. 1 <jsp: include> Action • <jsp: include> action – Enables dynamic content to be included in a JSP – More flexible than include directive • Requires more overhead when page contents change frequently 2002 Prentice Hall. All rights reserved.

10. 6. 1 <jsp: include> Action (cont. ) 2002 Prentice Hall. All rights reserved.

10. 6. 1 <jsp: include> Action (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 <!-- Fig. 10. 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& nbsp; On-Site Seminars Delivered Worldwide </p> Outline <p> <a href = "mailto: deitel@deitel. com"> deitel@deitel. com</a> 978. 579. 9911 490 B Boston Post Road, Suite 200, Sudbury, MA 01776 </p> </div> Fig. 10. 7 Banner (banner. html) to include across the top of the XHTML document created by Fig. 10. 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 <!-- Fig. 10. 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> <p><a href = "http: //www. deitel. com/intro. html"> Who we are </a></p> Fig. 10. 8 Table of contents (toc. html) to include down the left side of the XHTML document created by Fig. 10. <p><a href = "http: //www. deitel. com/index. html"> Home Page </a></p> <p>Send questions or comments about this site to <a href = "mailto: deitel@deitel. com"> deitel@deitel. com </a> Copyright 1995 -2002 by Deitel & Associates, Inc. All Rights Reserved. </p> 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 <!-- Fig. 10. 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 ); %> Fig. 10. 9 JSP clock 2. jsp Use Localeto to format include as. Data the main content in the XHTML with specified Data. Format document created by Fig. 10. <%-- end script --%> <%-- output date --%> <%= date. Format. format( new java. util. Date() ) %> </p> </td> </tr> </table> Lines 14 -20 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 <? 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. 10. 7: 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; } table, tr, td { font-size: . 9 em; border: 3 px groove; padding: 5 px; background-color: #dddddd; } </style> </head> <body> <table> <tr> <td style = <img src width alt = </td> Fig. 10 JSP include. jsp includes resources with <jsp: include> (Part 1). "width: 160 px; text-align: center" > = "images/logotiny. png" = "140" height = "93" "Deitel & Associates, Inc. Logo" /> 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 <td> <%-- include banner. html in this JSP --%> <jsp: include page = "banner. html" flush = "true" /> Outline Use JSP action to include banner. html </td> </tr> <td style = "width: 160 px"> <%-- include toc. html in this JSP --%> <jsp: include page = "toc. html" flush = "true" /> </td> <td style = "vertical-align: top"> <%-- include clock 2. jsp in this JSP --%> <jsp: include page = "clock 2. jsp" flush = "true" /> </td> </tr> </table> </body> </html> Use JSP action to include toc. html Fig. 10 JSP include. jsp includes resources with <jsp: include> (Part 2). Lines 38 -39 Use JSP action to include clock 2. jsp Line 48 Lines 55 -56 2002 Prentice Hall. All rights reserved.

Outline Fig. 10 JSP include. jsp includes resources with <jsp: include> (Part 3). 2002

Outline Fig. 10 JSP include. jsp includes resources with <jsp: include> (Part 3). 2002 Prentice Hall. All rights reserved.

10. 6. 2 <jsp: forward> Action • <jsp: forward> action – Enables JSP to

10. 6. 2 <jsp: forward> Action • <jsp: forward> action – Enables JSP to forward request to different resources • Can forwarded requests only resources in same context • <jsp: param> action – Specifies name/value pairs of information • Name/Value pairs are passed to other actions 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 <? 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. 10. 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" ); 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> <% // continue scriptlet Fig. 10. 11 JSP forward 1. jsp receives a first. Name parameter, adds a date to the request parameters and forwards the request to forward 2. jsp Forward request to for further processing forward 2. jsp (Part 1). Lines 22 -25 } // end if else { %> <%-- end scriptlet to insert fixed template data --%> 2002 Prentice Hall. All rights reserved.

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

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 <form action = "forward 1. jsp" method = "get"> <p>Type your first name and press Submit </p> Outline <p><input type = "text" name = "first. Name" /> <input type = "submit" value = "Submit" /> </p> </form> <% // continue scriptlet } // end else %> <%-- end scriptlet --%> </body> </html> <!-- end XHTML document --> Fig. 10. 11 JSP forward 1. jsp receives a first. Name parameter, adds a date to the request parameters and forwards the request to forward 2. jsp for further processing (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 <? 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 <!-- 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> <body> <p class = "big"> Hello <%= request. get. Parameter( "first. Name" ) %>, Your request was received and forwarded at </p> <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> Fig. 10. 12 JSP forward 2. jsp receives a request (from forward 1. jsp in this request example) and Receive from uses the request forward 1. jsp, then as part of getparameters first. Name the response to the parameter from request client (Part 1). Lines 23 -24 Line 31 Get data parameter from request 2002 Prentice Hall. All rights reserved.

36 37 38 </body> Outline </html> Fig. 10. 12 JSP forward 2. jsp receives

36 37 38 </body> Outline </html> Fig. 10. 12 JSP forward 2. jsp receives a request (from forward 1. jsp in this example) and uses the request parameters as part of the response to the client (Part 2). 2002 Prentice Hall. All rights reserved.

10. 6. 3 <jsp: plugin> Action • <jsp: plugin> action – Adds an applet

10. 6. 3 <jsp: plugin> Action • <jsp: plugin> action – Adds an applet or Java. Bean to a Web page – Also enables client to download and install Java Plug-in 2002 Prentice Hall. All rights reserved.

10. 6. 3 <jsp: plugin> Action (cont. ) 2002 Prentice Hall. All rights reserved.

10. 6. 3 <jsp: plugin> Action (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 33 34 35 // Fig. 10. 14: Shapes. Applet. java // Applet that demonstrates a Java 2 D General. Path. package com. deitel. advjhtp 1. jsp. applet; // Java core packages import java. applet. *; import java. awt. event. *; import java. awt. geom. *; // Java extension packages import javax. swing. *; Outline Create JApplet to embed in JSP public class Shapes. Applet extends JApplet { // initialize the applet public void init() { // obtain color parameters from XHTML file try { int red = Integer. parse. Int( get. Parameter( "red" ) ); int green = Integer. parse. Int( get. Parameter( "green" ) ); int blue = Integer. parse. Int( get. Parameter( "blue" ) ); Fig. 10. 14 An applet to demonstrate Set JApplet in <jsp: plugin> background Fig. 10. 15 color (Partbased 1). on parameter values Line 14 Lines 21 -27 Color background. Color = new Color( red, green, blue ); set. Background( background. Color ); } // if there is an exception while processing the color // parameters, catch it and ignore it catch ( Exception exception ) { // do nothing } } 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 public void paint( Graphics g ) { // create arrays of x and y coordinates int x. Points[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 }; int y. Points[] = { 0, 36, 54, 96, 72, 96, 54, 36 }; Outline // obtain reference to a Graphics 2 D object Graphics 2 D g 2 d = ( Graphics 2 D ) g; // create a star from a series of points General. Path star = new General. Path(); // set the initial coordinate of the General. Path star. move. To( x. Points[ 0 ], y. Points[ 0 ] ); // create the star--this does not draw the star for ( int k = 1; k < x. Points. length; k++ ) star. line. To( x. Points[ k ], y. Points[ k ] ); // close the shape star. close. Path(); 10. 14 An applet Use. Fig. General. Path demonstrate totodisplay several <jsp: plugin> in colored stars Fig. 10. 15 (Part 2). Lines 40 -66 // translate the origin to (200, 200) g 2 d. translate( 200, 200 ); // rotate around origin and draw stars in random colors for ( int j = 1; j <= 20; j++ ) { g 2 d. rotate( Math. PI / 10. 0 ); 2002 Prentice Hall. All rights reserved.

68 69 70 71 72 73 74 75 76 g 2 d. set. Color(

68 69 70 71 72 73 74 75 76 g 2 d. set. Color( new Color( ( int ) ( Math. random() * 256 ), ( int ) ( Math. random() * 256 ) ) ); g 2 d. fill( star ); // draw a filled star Outline Use General. Path to display several colored stars } } } Fig. 10. 14 An applet to demonstrate <jsp: plugin> in Fig. 10. 15 (Part 3). Lines 68 -73 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 <!-- Fig. 10. 15: plugin. jsp --> Outline <html> <head> <title>Using jsp: plugin to load an applet</title> </head> <body> <jsp: plugin type = "applet" code = "com. deitel. advjhtp 1. jsp. applet. Shapes. Applet" codebase = "/advjhtp 1/jsp" width = "400" height = "400"> <jsp: params> <jsp: param name = "red" value = "255" /> <jsp: param name = "green" value = "255" /> <jsp: param name = "blue" value = "0" /> </jsp: params> </jsp: plugin> </body> </html> Use jsp: plugin action to display JApplet in JSP Fig. 10. 15 action Using Use jsp: param <jsp: plugin> to specify JApplet to embed acolor Java 2 background applet in a JSP (Part 1). Lines 10 -22 Lines 16 -20 2002 Prentice Hall. All rights reserved.

Outline Fig. 10. 15 Using <jsp: plugin> to embed a Java 2 applet in

Outline Fig. 10. 15 Using <jsp: plugin> to embed a Java 2 applet in a JSP (Part 2). 2002 Prentice Hall. All rights reserved.

10. 6. 4 <jsp: use. Bean> Action • <jsp: use. Bean> action – Enables

10. 6. 4 <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 2002 Prentice Hall. All rights reserved.

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

10. 6. 4 <jsp: use. Bean> Action (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 33 34 35 Outline // Fig. 10. 17: Rotator. java // A Java. Bean that rotates advertisements. package com. deitel. advjhtp 1. jsp. beans; public class Rotator { private String images[] = { "images/jhtp 3. jpg", "images/xmlhtp 1. jpg", "images/ebechtp 1. jpg", "images/iw 3 htp 1. jpg", "images/cpphtp 3. jpg" }; private String links[] = { "http: //www. amazon. com/exec/obidos/ASIN/0130125075/" "deitelassociatin", "http: //www. amazon. com/exec/obidos/ASIN/0130284173/" "deitelassociatin", "http: //www. amazon. com/exec/obidos/ASIN/013028419 X/" "deitelassociatin", "http: //www. amazon. com/exec/obidos/ASIN/0130161438/" "deitelassociatin", "http: //www. amazon. com/exec/obidos/ASIN/0130895717/" "deitelassociatin" }; + + + Fig. 10. 17 Rotator bean that maintains a set of advertisements (Part 1). Lines 25 -28 private int selected. Index = 0; // returns image file name for current ad public String get. Image() { return images[ selected. Index ]; } // returns the URL for ad's corresponding Web site public String get. Link() { return links[ selected. Index ]; } Lines 31 -34 Return image file name for book cover image Return hyperlink to book at Amazon. com 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 42 // update selected. Index so next calls

36 37 38 39 40 41 42 // 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 Update Rotator so subsequent calls to get. Image and get. Link return information for different advertisements Fig. 10. 17 Rotator bean that maintains a set of advertisements (Part 2). Lines 38 -41 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" > Outline <!-- Fig. 10. 18: adrotator. jsp --> <jsp: use. Bean id = "rotator" scope = "application" class = "com. deitel. advjhtp 1. jsp. beans. Rotator" /> <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> <body> <p class = "big">Ad. Rotator Example</p> <p> Use jsp: use. Bean action to obtain reference to Rotator object Fig. 10. 18 JSP adrotator. jsp uses a Rotator bean to display a different advertisement on each request to the page (Part 1). Invoke Rotator’s Linesmethod 7 -8 next. Ad Line 22 Define hyperlink to Lines 29 -23 site Amazon. com <a href = "<jsp: get. Property name = "rotator" property = "link" />"> <img src = "<jsp: get. Property name = "rotator" property = "image" />" alt = "advertisement" /> </a> </p> 2002 Prentice Hall. All rights reserved.

36 37 </body> </html> Outline Fig. 10. 18 JSP adrotator. jsp uses a Rotator

36 37 </body> </html> Outline Fig. 10. 18 JSP adrotator. jsp uses a Rotator bean to display a different advertisement on each request to the page (Part 2). 2002 Prentice Hall. All rights reserved.

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

10. 6. 4 <jsp: use. Bean> Action (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 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; } // 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; } Guest. Bean defines three guest properties: first. Name, Fig. 10. 20 last. Name and email Guest. Bean stores information for one guest (Part 1). Line 6 // 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; } Outline // get the guest's email address public String get. Email() { return email; } } 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; Outline // 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; // 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" ); Fig. 10. 21 Guest. Data. Bean performs database access on behalf of guest. Book. Login. jsp (Part 1). Guest. Data. Bean connects to guestbook database Lines 22 -23 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(); 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 ); } Guest. Data. Bean provides methods get. Guest. List Fig. 10. 21 and add. Guest to Guest. Data. Bean manipulate database performs database access on behalf of guest. Book. Login. jsp (Part 2). return guest. List; } Lines 39 -68 // 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(); } Outline // process SQLException on close operation catch ( SQLException sql. Exception ) { sql. Exception. print. Stack. Trace(); } } } 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 <? 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; } table, tr, td { font-size: . 9 em; border: 3 px groove; padding: 5 px; background-color: #dddddd; } </style> </head> Outline page directive defines information that is globally available in JSP 10. 22 Use. Fig. jsp: use. Bean page actions. Java. Server to obtain references guest. Bookto Guest. Bean and Login. jsp enables Guest. Data. Bean objects 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 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 ) { Outline Set properties of Guest. Bean with request parameter values %> <%-- end scriptlet to insert fixed template data --%> <td> <input type = "text" name = "first. Name" /> </td> </tr> 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). <tr> <td>Last name</td> Line 36 <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 = "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 ); %> <%-- end scriptlet to insert jsp: forward action --%> <%-- forward to display guest book contents --%> <jsp: forward page = "guest. Book. View. jsp" /> <% // continue scriptlet } // end else 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). Forward request Line 93 to guest. Book. View. jsp %> <%-- 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 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. 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. *" %> Use page directive import to specify Java classes and packages that are used in JSP context <%-- 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" > <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> Use jsp: use. Bean Fig. 10. 23 action to obtain reference Java. Server page to Guest. Data. Bean guest. Book. View. jsp displays the contents of the guest book (Part 1). Lines 9 -10 Lines 13 -14 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 Outline <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> <% // 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(); Scriptlet displays last Fig. name 10. 23 and email name, first Java. Server page address for all guests guest. Book. View. jsp displays the contents of the guest book (Part 2). %> <%-- end scriptlet; insert fixed template data --%> Lines 50 -68 <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 Outline } // end while %> <%-- end scriptlet --%> </tbody> </table> </body> </html> 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> <body> <p class = "big. Red"> <% // scriptlet to determine exception type // and output beginning of error message if ( exception instanceof SQLException ) %> An SQLException Use page directive is. Error. Page to specify that guest. Book. Error. Page is an error page 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 Use implicit object exception to determine error to be displayed 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 Use implicit object exception to determine error to be displayed %> An exception <%-- end scriptlet to insert fixed template data --%> <%-- continue error message output --%> occurred while interacting with the guestbook database. </p> <p class = "big. Red"> The error message was: <%= exception. get. Message() %> </p> <p class = "big. Red">Please try again later</p> </body> 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 </html> 2002 Prentice Hall. All rights reserved.

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

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

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

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

10. 7 Directives • JSP directives – Messages to JSP container – Enable programmer

10. 7 Directives • JSP directives – Messages to JSP container – Enable programmer to: • Specify page settings • Include content from other resources • Specify custom-tag libraries – Delimited by <%@ and %> 2002 Prentice Hall. All rights reserved.

10. 7 Directives (cont. ) 2002 Prentice Hall. All rights reserved.

10. 7 Directives (cont. ) 2002 Prentice Hall. All rights reserved.

10. 7. 1 page Directive • JSP page directive – Specifies JSP’s global settings

10. 7. 1 page Directive • JSP page directive – Specifies JSP’s global settings in JSP container 2002 Prentice Hall. All rights reserved.

10. 7. 1 page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

10. 7. 1 page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

10. 7. 1 page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

10. 7. 1 page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

10. 7. 1 page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

10. 7. 1 page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

10. 7. 2 include Directive • JSP include directive – Includes content of another

10. 7. 2 include Directive • JSP include directive – Includes content of another resource at JSP translation time • Not as flexible as <jsp: include> 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 <? 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. 28: include. Directive. jsp --> Outline Reimplement include. jsp using include directives <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <title>Using the include directive </title> <style type = "text/css"> body { font-family: tahoma, helvetica, arial, sans-serif; } table, tr, td { font-size: . 9 em; border: 3 px groove; padding: 5 px; background-color: #dddddd; } </style> </head> <body> <table> <tr> <td style = <img src width alt = </td> Fig. 10. 28 JSP include. Directive. jsp demonstrates including content at translation-time with directive include (Part 1). Line 5 "width: 160 px; text-align: center" > = "images/logotiny. png" = "140" height = "93" "Deitel & Associates, Inc. Logo" /> 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 <td> <%-- include banner. html in this JSP --%> <%@ include file = "banner. html" %> Outline Use include directive to include banner. html </td> </tr> <td style = "width: 160 px"> <%-- include toc. html in this JSP --%> <%@ include file = "toc. html" %> </td> <td style = "vertical-align: top"> <%-- include clock 2. jsp in this JSP --%> <%@ include file = "clock 2. jsp" %> </td> </tr> </table> </body> </html> Fig. 10. 28 JSP include. Directive. Use include directive jsp demonstrates to include toc. html including content at translation-time with directive include 2). directive Use(Part include to include clock 2. jsp Line 38 Line 47 Line 54 2002 Prentice Hall. All rights reserved.

Outline Fig. 10. 28 JSP include. Directive. jsp demonstrates including content at translation-time with

Outline Fig. 10. 28 JSP include. Directive. jsp demonstrates including content at translation-time with directive include (Part 3). 2002 Prentice Hall. All rights reserved.

10. 8 Custom Tag Libraries • Custom-tag libraries – Encapsulates complex functionality for use

10. 8 Custom Tag Libraries • Custom-tag libraries – Encapsulates complex functionality for use in JSPs – Define custom tags • Used for creating dynamic content • Classes that implement interface Tag – Pacakge javax. servlet. jsp. tagext 2002 Prentice Hall. All rights reserved.

10. 8 Custom Tag Libraries (cont. ) 2002 Prentice Hall. All rights reserved.

10. 8 Custom Tag Libraries (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 <? 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. 30: custom. Tag. Welcome. jsp --> <!-- JSP that uses a custom tag to output content. --> Outline Use taglib directive to include use tags in tag library <%-- taglib directive --%> <%@ taglib uri = "advjhtp 1 -taglib. tld" prefix = "advjhtp 1" %> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <title>Simple Custom Tag Example </title> </head> <body> <p>The following text demonstrates a custom tag: </p> <h 1> <advjhtp 1: welcome /> </h 1> </body> </html> Fig. 10. 30 JSP custom. Tag. Welcome. jsp uses a simple custom tag. Use custom tag welcome to insert Linetext 9 in the JSP Line 20 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 Outline // Fig. 10. 31: Welcome. Tag. Handler. java // Custom tag handler that handles a simple tag. package com. deitel. advjhtp 1. jsp. taglibrary; // Java core packages import java. io. *; // Java extension packages import javax. servlet. jsp. *; import javax. servlet. jsp. tagext. *; public class Welcome. Tag. Handler extends Tag. Support { // Method called to begin tag processing public int do. Start. Tag() throws Jsp. Exception { // attempt tag processing try { // obtain Jsp. Writer to output content Jsp. Writer out = page. Context. get. Out(); Class Welcome. Tag. Handler implements interface Tag by extending class Tag. Support Fig. 10. 31 JSP container. Welcome. Tagcalls method do. Start. Tag when it encounters Handler custom tag the startinghandler. custom tag // output content out. print( "Welcome to JSP Tag Libraries!" ); } Line 12 Use custom tag handler’s page. Context to obtain Lines 15 -32 object JSP’s Jsp. Writer for outputting text Line 20 // rethrow IOException to JSP container as Jsp. Exception catch( IOException io. Exception ) { throw new Jsp. Exception( io. Exception. get. Message() ); } return SKIP_BODY; // ignore the tag's body } } 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 <? xml version = "1. 0" encoding = "ISO-8859 -1" ? > <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc. //DTD JSP Tag Library 1. 1//EN" "http: //java. sun. com/j 2 ee/dtds/web-jsptaglibrary_1_1. dtd" > <!-- a tag library descriptor --> Define custom-tag library descriptor file <taglib> <tlibversion>1. 0</tlibversion> <jspversion>1. 1</jspversion> <shortname>advjhtp 1</shortname> <info> A simple tab library for the examples </info> <!-- A simple tag that outputs content --> <tag> <name>welcome</name> Outline Fig. 10. 32 Custom tag element describes tag library descriptor welcome file custom tag advjhtp 1 taglib. tld. Specify custom tag Line name and 8 class <tagclass> com. deitel. advjhtp 1. jsp. taglibrary. Welcome. Tag. Handler </tagclass> Line 18 <bodycontent>empty</bodycontent> Lines 19 -23 <info> Inserts content welcoming user to tag libraries </info> </taglib> 2002 Prentice Hall. All rights reserved.

10. 8. 2 Custom Tag with Attributes • XHTML and JSP elements – Use

10. 8. 2 Custom Tag with Attributes • XHTML and JSP elements – Use attributes to customize functionality • Specify attributes for custom tags 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 <? 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. 10. 33: custom. Tag. Attribute. jsp --> <!-- JSP that uses a custom tag to output content. --> <%-- taglib directive --%> <%@ taglib uri = "advjhtp 1 -taglib. tld" prefix = "advjhtp 1" %> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <head> <title>Specifying Custom Tag Attributes </title> </head> <body> <p>Demonstrating an attribute with a string value </p> <h 1> <advjhtp 1: welcome 2 first. Name = "Paul" /> </h 1> <p>Demonstrating an attribute with an expression value </p> <h 1> <%-- scriptlet to obtain "name" request parameter --%> <% String name = request. get. Parameter( "name" ); %> Fig. 10. 33 Specifying attributes Use welcome 2 tag to for a text custom tagthat insert in JSP 1). based on is(Part customized first. Name attribute Lines 20 and 30 value <advjhtp 1: welcome 2 first. Name = "<%= name %>" /> </h 1> </body> </html> 2002 Prentice Hall. All rights reserved.

Outline Fig. 10. 33 Specifying attributes for a custom tag (Part 2). 2002 Prentice

Outline Fig. 10. 33 Specifying attributes for a custom tag (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 Outline // Fig. 10. 34: Welcome 2 Tag. Handler. java // Custom tag handler that handles a simple tag. package com. deitel. advjhtp 1. jsp. taglibrary; // Java core packages import java. io. *; // Java extension packages import javax. servlet. jsp. *; import javax. servlet. jsp. tagext. *; public class Welcome 2 Tag. Handler extends Tag. Support { private String first. Name = ""; // Method called to begin tag processing public int do. Start. Tag() throws Jsp. Exception { // attempt tag processing try { // obtain Jsp. Writer to output content Jsp. Writer out = page. Context. get. Out(); Define first. Name attribute Fig. 10. 34 Welcome 2 Tag. Handler custom tag handler for a tag with an attributevalue (Partas 1). Use first. Name part of text output by custom tag Line 13 // output content out. print( "Hello " + first. Name + ", Welcome to JSP Tag Libraries!" ); Lines 24 -25 } // rethrow IOException to JSP container as Jsp. Exception catch( IOException io. Exception ) { throw new Jsp. Exception( io. Exception. get. Message() ); } return SKIP_BODY; } // ignore the tag's body 2002 Prentice Hall. All rights reserved.

36 37 38 39 40 41 // set first. Name attribute to the users

36 37 38 39 40 41 // set first. Name attribute to the users first name public void set. First. Name( String username ) { first. Name = username; } } Outline Corresponding set accessor method for first. Name attribute value Fig. 10. 34 Welcome 2 Tag. Handler custom tag handler for a tag with an attribute (Part 2). Lines 37 -40 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 Outline <!-- A tag with an attribute --> <tag> <name>welcome 2</name> <tagclass> com. deitel. advjhtp 1. jsp. taglibrary. Welcome 2 Tag. Handler </tagclass> <bodycontent>empty</bodycontent> <info> Inserts content welcoming user to tag libraries. Uses attribute "name" to insert the user's name. </info> <attribute> <name>first. Name</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> Fig. 10. 35 Element Introduce element attribute tag for the for specifyingwelcome 2 the characteristics custom of a tag’s attributes tag. Lines 16 -20 2002 Prentice Hall. All rights reserved.

10. 8. 3 Evaluating the Body of a Custom Tag • Custom tags –

10. 8. 3 Evaluating the Body of a Custom Tag • Custom tags – Particularly useful for processing element body 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" > Outline <!-- custom. Tag. Body. jsp --> <%-- taglib directive --%> <%@ taglib uri = "advjhtp 1 -taglib. tld" prefix = "advjhtp 1" %> <html xmlns = "http: //www. w 3. org/1999/xhtml" > <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> Fig. 10. 36 Using a custom tag that interacts with its body (Part 1). <body> <p style = "font-size: 2 em">Guest List</p> 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 53 54 55 56 <table> <thead> <th style = "width: 100 px">Last name</th> <th style = "width: 100 px">First name</th> <th style = "width: 200 px">Email</th> </thead> Outline <%-- guestlist custom tag --%> <advjhtp 1: guestlist> <tr> <td><%= last. Name %></td> <td><%= first. Name %></td> <a href = "mailto: <%= email %>"> <%= email %></a> </td> </tr> </advjhtp 1: guestlist> </table> </body> Use custom guestlist tag to display last name, first and Using email a Fig. name 10. 36 custom tag that interacts with its body (Part 2). Lines 41 -52 </html> 2002 Prentice Hall. All rights reserved.

Outline Fig. 10. 36 Using a custom tag that interacts with its body (Part

Outline Fig. 10. 36 Using a custom tag that interacts with its body (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 // Fig. 10. 37: Guest. Book. Tag. java // Custom tag handler that reads information from the guestbook // database and makes that data available in a JSP. package com. deitel. advjhtp 1. jsp. taglibrary; Outline // Java core packages import java. io. *; import java. util. *; // Java extension packages import javax. servlet. jsp. *; import javax. servlet. jsp. tagext. *; // Deitel packages import com. deitel. advjhtp 1. jsp. beans. *; public class Guest. Book. Tag extends Body. Tag. Support { private String first. Name; private String last. Name; private String email; Fig. 10. 37 Guest. Book. Tag custom tag handler (Part 1). private Guest. Data. Bean guest. Data; private Guest. Bean guest; private Iterator iterator; // Method called to begin tag processing public int do. Start. Tag() throws Jsp. Exception { // attempt tag processing try { guest. Data = new Guest. Data. Bean(); List list = guest. Data. get. Guest. List(); iterator = list. iterator(); 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 if ( iterator. has. Next() ) { process. Next. Guest(); return EVAL_BODY_TAG; } else return SKIP_BODY; // continue body Outline Extract information for first guest and create variable processing containing that information in the JSP’s Page. Context // terminate body processing } // if any exceptions occur, do not continue processing // tag's body catch( Exception exception ) { exception. print. Stack. Trace(); return SKIP_BODY; // ignore the tag's body } Fig. 10. 37 Guest. Book. Tag custom tag handler (Part 2). } // process body and determine if body processing // should continue public int do. After. Body() { // attempt to output body data try { body. Content. write. Out( get. Previous. Out() ); } Lines 36 -27 Method do. After. Body can be called many times to process the 55 tag body of a. Line custom Line 59 Process the first client’s data // if exception occurs, terminate body processing catch ( IOException io. Exception ) { io. Exception. print. Stack. Trace(); return SKIP_BODY; // terminate body processing } body. Content. clear. Body(); Line 68 Ensure that the outputted body content is not processed in subsequent call to 2002 Prentice Hall. method do. After. Body 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 Extract information. Outline for next guest (if available) if ( iterator. has. Next() ) { process. Next. Guest(); return EVAL_BODY_TAG; } else return SKIP_BODY; // continue body processing // terminate body processing } // obtains the next Guest. Bean and extracts its data private void process. Next. Guest() { // get next guest = ( Guest. Bean ) iterator. next(); page. Context. set. Attribute( "first. Name", guest. get. First. Name() ); page. Context. set. Attribute( "last. Name", guest. get. Last. Name() ); page. Context. set. Attribute( "email", guest. get. Email() ); Fig. 10. 37 Guest. Book. Tag custom tag handler (Part 3). Lines 70 -71 } } 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 // Fig. 10. 38: Guest. Book. Tag. Extra. Info. java // Class that defines the variable names and types created by // custom tag handler Guest. Book. Tag. package com. deitel. advjhtp 1. jsp. taglibrary; Outline // Java core packages import javax. servlet. jsp. tagext. *; public class Guest. Book. Tag. Extra. Info extends Tag. Extra. Info { // method that returns information about the variables // Guest. Book. Tag creates for use in a JSP public Variable. Info [] get. Variable. Info( Tag. Data tag. Data ) { Variable. Info first. Name = new Variable. Info( "first. Name", "String", true, Variable. Info. NESTED ); Variable. Info last. Name = new Variable. Info( "last. Name", "String", true, Variable. Info. NESTED ); Variable. Info email = new Variable. Info( "email", "String", true, Variable. Info. NESTED ); Variable. Info varable. Info [] = { first. Name, last. Name, email }; Fig. 10. 38 Guest. Book. Tag. Extra. Info used by the container to define scripting variables in a JSP that uses the guestlist custom tag. return varable. Info; } } 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 <!-- A tag that iterates over an Array. List of Guest. Bean --> <!-- objects, so they can be output in a JSP --> <tag> <name>guestlist</name> <tagclass> com. deitel. advjhtp 1. jsp. taglibrary. Guest. Book. Tag </tagclass> <teiclass> com. deitel. advjhtp 1. jsp. taglibrary. Guest. Book. Tag. Extra. Info </teiclass> Outline Specify custom tag’s Extra. Info class <bodycontent>JSP</bodycontent> <info> Iterates over a list of Guest. Bean objects </info> </tag> Fig. 10. 39 Element tag for the guestlist custom tag. Lines 10 -12 2002 Prentice Hall. All rights reserved.