SECTION 8: THE JAVA SERVER PAGES (JSP) TECHNOLOGY MODEL

Section 8
8.1 Write the opening and closing tags for the following JSP tag types:
  • Directive
  • Declaration
  • Scriptlet
  • Expression
8.2 Given a type of JSP tag, identify correct statements about its purpose or use.
8.3 Given a JSP tag type, identify the equivalent XML-based tags.
8.4 Identify the page directive attribute, and its values, that:
  • Import a Java class into the JSP page
  • Declare that a JSP page exists within a session
  • Declare that a JSP page uses an error page
  • Declare that a JSP page is an error page
8.5 Identify and put in sequence the following elements of the JSP page lifecycle:
  • Page translation
  • JSP page compilation
  • Load class
  • Create instance
  • Call jspInit
  • Call _jspService
  • Call jspDestroy
8.6 Match correct descriptions about purpose, function, or use with any of the following implicit objects:
  • request
  • response
  • out
  • session
  • config
  • application
  • page
  • pageContext
  • exception
8.7 Distinguish correct and incorrect scriptlet code for:
  • A conditional statement;
  • An iteration statement

Section 8 - The Java Server Pages (JSP) Technology Model

8.1 Write the opening and closing tags for the following JSP tag types:

  1. Directive
  2. Declaration
  3. Scriptlet
  4. Expression

8.2 Given a type of JSP tag, identify correct statements about its purpose or use.

8.3 Given a JSP tag type, identify the equivalent XML-based tags.

taglib directive doesn't have XML equivalent, it is included in <jsp:root> element as a xmlns element. <%@taglib uri=http://my.com/my.tld prefix="my" %> will become <jsp:root xmlns:my=http://my.com/my.tld> </jsp:root> Template text should use <jsp:text> element.

8.4 Identify the page directive attribute, and its values, that:

  1. Import a Java class into the JSP page
    <%@ page import="java.util.Date" %> (Multiple declarations are separated by comma)
  2. Declare that a JSP page exists within a session
    <%@ page session="true"%>
  3. Declare that a JSP page uses an error page
    <%@ page errorPage="errorPage.jsp" %>
  4. Declare that a JSP page is an error page
    <%@ page isErrorPage="true" %>

8.5 Identify and put in sequence the following elements of the JSP page lifecycle:

  1. Page translation
  2. JSP page compilation
  3. Load class
  4. Create instance
  5. Call jspInit
  6. Call _jspService
  7. Call jspDestroy

The above order is already correct.

Basically there are two phases, translation and execution. During the translation phase the container locates or creates the JSP page implementation class that corresponds to a given JSP page. This process is determined by the semantics of the JSP page. The container interprets the standard directives and actions, and the custom actions referencing tag libraries used in the page. A tag library may optionally provide a validation method to validate that a JSP page is correctly using the library. A JSP container has flexibility in the details of the JSP page implementation class that can be used to address quality-of-service --most notably performance--issues.

During the execution phase the JSP container delivers events to the JSP page implementation object. The container is responsible for instantiating request and response objects and invoking the appropriate JSP page implementation object. Upon completion of processing, the response object is received by the container for communication to the client.

8.6 Match correct descriptions about purpose, function, or use with any of the following implicit objects:

  1. request
  2. response
  3. out
  4. session
  5. config
  6. application
  7. page
  8. pageContext
  9. exception
Purpose, function or use of the implicit objects
Implicit Object Type Purpose/Useful methods Scope
request Subclass of ServletRequest The request.getAttribute, setAttribute, getParameter, getParameterNames, getParameterValues request
response Subclass of ServletResponse The response page
out JspWriter Object for writing to the output stream, flush, getBuffreSize page
session HttpSession created as long as <% page session="false" %> is not used. Valid for Http protocol only. getAttribute, setAttribute session
config ServletConfig Specific to a servlet instance. Same as Servlet.getServletConfig() getInitParameter, getInitParameterNames page
application ServletContext Available to all Servlets (application wide). Provides access to resources of the servlet engine (resources, attributes, context params, request dispatcher, server info, URL & MIME resources). application
page Object this instance of the page(equivalent servlet instance "this") page
pageContext PageContext provides a context to store references to objects used by the page, encapsulates implementation-dependent features, and provides convenience methods. page
exception java.lang.Throwable created only if <%@page isErrorPage="true"%> page

PageContext provides a number of facilities to the page/component implementor including

The methods related to attributes are:

setAttribute(), 
getAttribute(), 
removeAttribute() - deals page scope
findAttribute(), - looks in all scopes 
int getAttributesScope()
getAttributeNamesInScope(int scope)

The following methods provide convenient access to implicit objects:

getOut(), 
getException(), 
getPage() 
getRequest(), 
getResponse(), 
getSession(), 
getServletConfig() 
getServletContext()

The following methods provide support for forwarding, inclusion and error handling:

forward(relativeURl), 
include(relativeURl), 
handlePageException(Exception/Throwable).

8.7 Distinguish correct and incorrect scriptlet code for:

  1. A conditional statement;
  2. An iteration statement

Conditional Statement

<% if (event.equals("ENTER_RECORD")) { %>
<jsp:include page="enterRecord.jsp" flush=true"/>
<% } else if (event.equals ("NEW_RECORD")) { %>
<jsp:include page="newRecord.jsp" flush="true/>
<% } else { %>
<jsp:include page="default.jsp" flush = true"/>
<% } %>

Iterative Statement

<% Hashtable h = (Hashtable) session.getAttribute("charges");
if (h != null) { %>
<ul>
<%
Enumeration charges = h.keys();
While (charges.hasMoreElements()) {
    String proj = (String) charges.nextElement();
    Charge ch = (Charge) h.get(proj);
%>
<li>
    name = <%= ch.getName() %>
  , project = <%= proj %>
  , hours = <%= ch.getHours() %>
  , date = <%= ch.getDate() %>
<% } %>
</ul>
<% } %>

Return to top