1 / 98

JavaServer Pages (JSP)

27. JavaServer Pages (JSP). If it’s a good script, I’ll do it and if it’s a bad script and they pay me enough, I’ll do it. George Burns A fair request should be followed by the deed in silence. Dante Alighieri

jeff
Télécharger la présentation

JavaServer Pages (JSP)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 27 • JavaServer Pages (JSP)

  2. If it’s a good script, I’ll do it and if it’s a bad script and they pay me enough, I’ll do it. George Burns A fair request should be followed by the deed in silence. Dante Alighieri Talent is a question of quantity. Talent does not write one page: it writes three hundred. Jules Renard Every action must be due to one or other of seven causes: chance, nature, compulsion, habit, reasoning, anger, or appetite. Aristotle

  3. OBJECTIVES In this chapter you will learn: • The differences between servlets and JSPs. • To create and deploy JavaServer Pages. • To use JSP’s implicit objects and scriptlets to create dynamic Web pages. • To specify global JSP information with directives. • To use actions to manipulate JavaBeans in a JSP, to include resources dynamically and to forward requests to other JSPs.

  4. 27.1   Introduction • 27.2   JavaServer Pages Overview • 27.3   First JSP Example • 27.4   Implicit Objects • 27.5   Scripting • 27.5.1  Scripting Components • 27.5.2  Scripting Example • 27.6   Standard Actions • 27.6.1  <jsp:include> Action • 27.6.2  <jsp:forward> Action • 27.6.3  <jsp:useBean> Action • 27.7   Directives • 27.7.1  page Directive • 27.7.2  include Directive • 27.8   Case Study: Guest Book • 27.9   Wrap-Up • 27.10   Internet and Web Resources

  5. 27.1 Introduction • JavaServer Pages • Extension of Servlet technology • Separate the presentation from the business logic • Simplify the delivery of dynamic Web content • Reuse existing Java components • JavaBean • Custom-tag libraries • Encapsulate complex functionality • Classes and interfaces specific to JSP • Package javax.servlet.jsp • Package javax.servlet.jsp.tagext

  6. 27.2 JavaServer Pages Overview • Key components • Directives • Actions • Scripting elements • Tag libraries

  7. 27.2 JavaServer 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

  8. 27.2 JavaServer 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

  9. 27.2 JavaServer Pages Overview (Cont.) • Scripting elements • Enable programmers to insert Java code in JSPs • Performs request processing • Interacts with page elements and other components to implement dynamic pages • Scriptlets • One kind of scripting element • Contain code fragments • Describe the action to be performed in response to user request

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

  11. 27.2 JavaServer Pages Overview (Cont.) • JSPs • Look like standard XHTML or XML • Normally include XHTML or XML 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

  12. Software Engineering Observation 27.1 • Literal text in a JSP becomes string literals in the servlet that represents the translated JSP.

  13. 27.2 JavaServer Pages Overview (Cont.) • When server receive the first JSP request • JSP container translates a JSP into a servlet • Handle the current and future requests • Code that represents the JSP • Placed in servlet’s _jspService method • JSP errors • Translation-time errors • Occur when JSPs are translated into servlets • Request-time errors • Occur during request processing • Methods jspInit and jspDestroy • Container invokes them when initializing and terminating a JSP • Defined in JSP declarations

  14. Performance Tip 27.1 • Some JSP containers translate JSPs to servlets at installation time. This eliminates the translation overhead for the first client that requests each JSP.

  15. 27.3 First JSP Example • Simple JSP example (Fig. 27.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

  16. meta element refreshes the Web page every 60 seconds Creates Date object that is converted to a String implicitly and displayed in paragraph (p) element Outline • clock.jsp • (1 of 2) • Line 9 • Line 24

  17. Outline • clock.jsp • (2 of 2) • Program output

  18. Software Engineering Observation 27.2 • JavaServer Pages are easier to implement than servlets when the response to a client request consists primarily of markup that remains constant between requests.

  19. Software Engineering Observation 27.3 • The JSP container converts the result of every JSP expression into a string that is output as part of the response to the client.

  20. 27.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

  21. 27.4 Implicit Objects (Cont.) • 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

  22. Fig. 27.2 | JSP implicit objects. (1 of 2)

  23. Fig. 27.2 | JSP implicit objects. (2 of 2)

  24. 27.5 Scripting • Scripting • Dynamically generated content • Insert Java code and logic in JSP using scripting

  25. 27.5.1 Scripting Components • JSP scripting components • Scriptlets (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 %>) • Declarations (delimited by <%! and %>) • Escape sequences

  26. Common Programming Error 27.1 • Placing a JSP comment or XHTML comment inside a scriptlet is a translation-time syntax error that prevents the JSP from being translated properly.

  27. Software Engineering Observation 27.4 • JSPs should not store client state information in instance variables. Rather, they should use the JSP implicit session object. For more information on how to use the session object, visit Sun’s J2EE tutorial at java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html.

  28. Fig. 27.3 | JSP escape sequences.

  29. 27.5.2 Scripting Example • Demonstrate basic scripting capabilities • Responding to get requests

  30. Software Engineering Observation 27.5 • Scriptlets, expressions and fixed-template data can be intermixed in a JSP to create different responses based on the information in a request.

  31. JSP expression Scriptlet used to insert Java code Use request implicit object to get parameter Outline • welcome.jsp • (1 of 3) • Lines 17-23 • Line 19 • Line 26

  32. Scriptlets used to insert Java code Outline • welcome.jsp • (2 of 3) • Lines 30-35 and lines 45-49

  33. Outline • welcome.jsp • (3 of 3) • Program output

  34. Error-Prevention Tip 27.1 • It is sometimes difficult to debug errors in a JSP, because the line numbers reported by a JSP container normally refer to the servlet that represents the translated JSP, not the original JSP line numbers. Program development environments enable JSPs to be compiled in the environment, so you can see syntax error messages. These messages include the statement in the servlet that represents the translated JSP, which can be helpful in determining the error.

  35. Error-Prevention Tip 27.2 • Many JSP containers store the source code for the servlets that represent the translated JSPs. For example, the Tomcat installation directory contains a subdirectory called work in which you can find the source code for the servlets translated by Tomcat. Recall from Chapter 26 that the log files located in the logs subdirectory of the Tomcat installation directory are also helpful for determining the errors.

  36. Error-Prevention Tip 27.3 • Always put the closing brace for the if statement and the else in the same scriptlet.

  37. 27.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 JavaBeans • JSP containers process actions at request time • Delimited by <jsp:action> and </jsp:action>

  38. Fig. 27.5 | JSP standard actions. (1 of 2)

  39. Fig. 27.5 | JSP standard actions. (2 of 2)

  40. 27.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

  41. Software Engineering Observation 27.6 • According to the JavaServer Pages 2.0 specification, a JSP container is allowed to determine whether a resource included with the include directive has changed. If so, the container can recompile the JSP that included the resource. However, the specification does not provide a mechanism to indicate a change in an included resource to the container.

  42. Performance Tip 27.2 • The <jsp:include> action is more flexible than the include directive, but requires more overhead when page contents change frequently. Use the <jsp:include> action only when dynamic content is necessary.

  43. Common Programming Error 27.2 • Specifying in a <jsp:include> action a page that is not part of the same Web application is a request-time error—the <jsp:include> action will not include any content.

  44. Fig. 27.6 | Action <jsp:include> attributes.

  45. Outline • include.jsp • (1 of 3)

  46. Use JSP include action to include banner.html Use JSP include action to include toc.html Use JSP include action to include clock2.jsp Outline • include.jsp • (2 of 3) • Lines 38-39 • Line 45 • Lines 49-50

  47. Outline • include.jsp • (3 of 3) • Program output

  48. Outline • banner.html

  49. Outline • toc.html

  50. Use request object’s getLocale method to obtain the client’s Locale Invoke DateFormatstatic method getDateTimeInstance to obtain a DataFormat object for the specified Locale Format Date with specified DataFormat Outline • clock2.jsp • Line 14 • Lines 17-20 • Line 25

More Related