1 / 25

Java Server Pages (JSP)

Java Server Pages (JSP).

mead
Download Presentation

Java Server 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. Java Server Pages (JSP) Anatomy of a JSP pageJSP Components Directivespageincludetaglib Standard Actions<jsp:include><jsp:forward>, <jsp:param> <jsp:plugin><jsp:useBean>, <jsp:setProperty>, <jsp:getProperty> ScriptingScriptlets (delimited by <% and %>) Comments (delimited by <%-- and --%>) Expressions (delimited by <%= and %>) Declarations (delimited by <%! and %>) Escape sequences Tag LibrariesImplicit ObjectsJavaBeans

  2. Anatomy of a JSP page

  3. JSP Components • Directives • Standard Actions • Scriptlets • Tag libraries

  4. Directives • A message to the container that lets you specify: • Page settings • Content to include from other resources • Custom tag libraries used in the JSP • Delimited by <%@ and %>

  5. page Directive • Specifies JSP’s global settings in JSP container • First line of a JSP file • Example:<%@ page language=“java” contentType=“text/html” %>

  6. page Directive (cont.)

  7. page Directive (cont.)

  8. page Directive (cont.)

  9. include Directive • Includes content of another resource at JSP translation time • Not as flexible as <jsp:include> action • Example: <tr> <td style = "width: 160px"> <%-- include toc.html in this JSP --%> <%@include file ="toc.html"%> </td> <td style = "vertical-align: top"> <%-- include clock2.jsp in this JSP --%> <%@include file ="clock2.jsp" %> </td> </tr>

  10. Standard Actions • Predefined JSP tags that encapsulate functionality • 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> • Often performed based on information from client request • Can be used to create Java objects for use in scriptlets

  11. Standard Actions (cont.)

  12. Standard Actions (cont.)

  13. <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 • Example:<jsp:include page="banner.html" flush="true" />

  14. <jsp:forward> & <jsp:param> Actions • <jsp:forward> action • Enables JSP to forward request to different resource • <jsp:param> action • Specifies name/value pairs of information to pass to other actions • Example:<jsp:forward page=“forward2.jsp"> <jsp:param name = "date" value = "<%= new java.util.Date() %>" /> </jsp:forward>

  15. <jsp:plugin> Action • Adds an applet or JavaBean to a Web page <html> <head> <title>Using jsp:plugin to load an applet</title> </head> <body> <jsp:plugin type = "applet" code = "com.deitel.advjhtp1.jsp.applet.ShapesApplet" codebase = "/advjhtp1/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>

  16. <jsp:plugin> Action (cont.)

  17. <jsp:useBean>, <jsp:setProperty> & <jsp:getProperty> Actions • <jsp:useBean> action • Enables JSP to manipulate Java object • Creates Java object or locates an existing object for use in JSP • See below for more on JavaBeans

  18. <jsp:useBean>, <jsp:setProperty> & <jsp:getProperty> Actions (cont.)

  19. <jsp:useBean>, <jsp:setProperty> & <jsp:getProperty> Actions

  20. Scripting • How JSP programmers can insert Java code and logic • JSP scripting components • Scriptlets (delimited by <% and %>) • Comments (delimited by <%-- and --%>) • Expressions (delimited by <%= and %>) • Declarations (delimited by <%! and %>) • Escape sequences

  21. Scripting Escape Sequences

  22. Implicit Objects • Implicit Objects make standard servlet objects (such as HttpServletRequest and HttpServletResponse) available to a JSP • Four scopes for Implicit Objects & JavaBeans • 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

  23. Implicit Objects (cont.)

  24. Implicit Objects (cont.)

  25. JavaBeans • Not to be confused with EnterpriseJavaBeans (EJB) • When is an object a Bean? “A simple Java object is a Java bean when all of the object’s data fields are private and are only accessible through accessor methods. That’s it! These requirements should be followed in object-oriented programming anyway. It’s just that JavaBeans forces you to use proper object-oriented programming techniques.” • Other constraints • No-arg constructor • If you do not include a no-arg constructor and the container needs to instantiate & initialize an object, the container will use the superclass no-arg constuctor (Object if no other class is explicitly extended) • Must be in a package for use by the container • Differences between beans & EJBs • Ordinary beans normally reside on same machine; EJBs support a distributed, transactional multitier architecture • EJBs managed by EJB container • Ordinary beans can be created using a “Bean box” tool that implements the java.beans API.

More Related