E N D
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
JSP Components • Directives • Standard Actions • Scriptlets • Tag libraries
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 %>
page Directive • Specifies JSP’s global settings in JSP container • First line of a JSP file • Example:<%@ page language=“java” contentType=“text/html” %>
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>
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
<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" />
<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>
<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>
<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
<jsp:useBean>, <jsp:setProperty> & <jsp:getProperty> Actions (cont.)
<jsp:useBean>, <jsp:setProperty> & <jsp:getProperty> Actions
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
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
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.