| Section 8 |
|---|
8.1 Write the opening and closing tags for the following JSP tag types:
|
| 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:
|
8.5 Identify and put in sequence the following elements of the JSP page lifecycle:
|
8.6 Match correct descriptions about purpose, function, or use with any of the following implicit objects:
|
8.7 Distinguish correct and incorrect scriptlet code for:
|
8.1 Write the opening and closing tags for the following JSP tag types:
Directives
Declaration
Scriptlet
Expresson
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:
8.5 Identify and put in sequence the following elements of the JSP page lifecycle:
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:
| 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:
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>
<% } %>