Java Reference
In-Depth Information
JavaServer Pages are a way for HTML specialists and scripting specialists to write dynamic HTML pages. These
pages use HTML-like JSP tags that can contain or call Java code. The JSP-specific elements of the HTML page
are converted to Java servlets.
The first time a JSP is called in a Web server, it is run through an interpreter. The interpreter converts the file into
a Java source file, translating the JSP tags into Java code. The Java file is then compiled and is structurally
equivalent to a servlet. In fact, when a JSP subsequently runs, its lifecycle is exactly the same as the servlet and
can basically be managed in the same way by the Web container.
JSPs and servlets share an architectural model as well as many of the same classes. The few modifications to the
JSP model, as well as the support classes for the technology, are located in the javax.servlet.jsp package. For
JSPs, the interfaces JspPage and HttpJspPage define the core functional model. However, although there are
different interfaces that specify JSP structure, the type of methods are the same— jspInit , jspDestroy and
_jspService are the analogs of the servlet init , destroy , and service methods.
The JSP API also defines a set of classes that allow the creation of custom tag libraries. This technology allows
you to extend JSPs with Java code. You only need to write three components: a Java class containing the Java
functionality to implement, and an XML file that functions as a deployment descriptor, stating basic information
about it, such as attributes that it can use. The third component is the use in the JSP itself, where the JSP scripter
specifies the Java class file, the XML file, and any other information for implementation, such as attributes.
Pattern Use
Template Method (see page 131): The servlet API comes close to providing an example of the Template
Method design pattern in a couple of places, falling short in only one key area; it actually provides a default
implementation for the methods. The methods in the HttpServlet that come close to this pattern implementation
are provided in the following list:
The service method branches to the doXxx methods. Developers usually override one or more of the doXxx
methods to implement servlet behavior.
The init(ServletConfig ) method calls the init method. Developers are supposed to override the init method
if there is any required initialization behavior.
Session (see page 220): The servlet API provides two mechanisms in support of the Session pattern—the Cookie
class and the HttpSession interface. As its name suggests, the Cookie class represents HTTP cookies, which
allow the servlet API to use session information stored on a Web client. The HttpSession interface is used for
server-side storage of session information.
Observer (see page 94): Like many Java APIs, Servlets use the event handling model. By extension, this means
they use the Observer pattern. For Servlets, the Observer is used to notify listeners of changes to HttpSession and
ServletContext objects. Table 8-1 shows the Servlet listener interfaces which can be used to create observers in a
Web application::
Table Table 8-1 Interfaces and corresponding purpose
Interface
Purpose
HttpSessionActivationListener
Session activation or passivation
HttpSessionAttributesListener
Change in attributes for a session
HttpSessionBindingListener
Notifies an object that it is being bound to or unbound from a session
HttpSessionListener
Session creation or destruction
ServletContextAttributesListener
Change in attributes for the servlet context
ServletContextListener
S ervlet creation and destruction
 
Search WWH ::




Custom Search