Java SCWCD Exam Cram Notes : The Jsp Model
Section 8: The JSP Model
8.1 Write the opening and closing tags for the following JSP tag types:
- Directive,
- Declaration,
- Scriptlet ,
- Expression
- Directive - <%@ Directive {attribute="value"}* %>
-
Declaration - <%! JavaDeclaration %>
- Scriptlet - <% JavaStatements %>
- Expression - <%= JavaExpression %>
8.2 Given a type of JSP tag, identify correct statements about its purpose or use
- Directive - controls translation and compilation phase.
- Declaration - declare a java variable or method. No output produced. Class
- level declarations.
- Scriptlet - valid java statements. No output produced unless out is used
- Expression - evaluation of expression to a String and then included in the output.
8.3 Given a JSP tag type, identify the equivalent XML-based tags.
- <%@ page ...%> == <jsp:directive.page .../>
- <%! ...%> == <jsp:declaration> ... </jsp:declaration>
- <% ...%> == <jsp:scriptlet> ... </jsp:scriptlet>
- <%= ...%> == <jsp:expression> ... </jsp:expression>
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:
-
Import a Java class into the JSP page <%@ page import="java.util.Date" %>
(Multiple declarations are separated by comma)
-
Declare that a JSP page exists within a session <%@ page session="true"%>
-
Declare that a JSP page uses an error page <%@ page errorPage="errorPage.jsp" %>
-
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:
-
Page translation
-
JSP page compilation
-
Load class
-
Create instance
-
Call jspInit
-
Call _jspService
-
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:
- request
- response
- out
- session
- config
- application
- page
- pageContext
- exception
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, getBufferSize | 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 providesconvenience methods. | page |
exception | java.lang.Throwable | created only if <%@ page isErrorPage="true" %> | page |
PageContext provides a number of facilities to the page/component author and page implementor including
- a single API to manage the various scoped namespaces
- a number of convenience API's to access various public objects
- a mechanism to obtain the JspWriter for output
- a mechanism to manage session usage by the page
- a mechanism to expose page directive attributes to the scripting environment
- mechanisms to forward or include the current request to other active compo-nents in the application
- a mechanism to handle errorpage exception processing
The methods related to attributes are:
setAttribute(), getAttribute(), removeAttribute() - deals page scope
findAttribute(), - looks in all scopes
int getAttributesScope() and getAttributeNamesInScope(int scope)
The following methods provide convenient access to implicit objects:
getOut(), getException(), getPage() getRequest(), getResponse(),
getSession(), getServletConfig() and getServletContext().
The following methods provide support for forwarding, inclusion and error handling:
forward(relativeURl), include(relativeURl), and handlePageException(Exception/Throwable).
8.7 Distinguish correct and incorrect scriptlet code for:
-
A conditional statement;
- An iteration statement
a) 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"/>
<% } %>
b) 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>
<%}%>