Describe the purpose and event sequence of the JSP page life cycle: (1) JSP page translation, (2) JSP page compilation, (3) load class, (4) create instance, (5) call the jspInit method, (6) call the _jspService method, and (7) call the jspDestroy method.

JSP page translation.

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 acting on the XML View of a JSP page, to validate that a JSP page is correctly using the library.

The translation of a JSP source page into its implementation class can occur at any time between initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for the target JSP page.

The JSP container creates a JSP page implementation class for each JSP page. The name of the JSP page implementation class is implementation dependent. The JSP Page implementation object belongs to an implementation-dependent named package. The package used may vary between one JSP and another, so minimal assumptions should be made. As of JSP 2.0, it is illegal to refer to any classes from the unnamed (a.k.a. default) package. This may result in a translation error on some containers,

The contract on the JSP page implementation class:

Additionally, it is the responsibility of the JSP page author that the provided superclass satisfies:
A JSP container may give a fatal translation error if it detects that the provided superclass does not satisfy these requirements, but most JSP containers will not check them.

JSP page compilation.

A JSP page may be compiled into its implementation class plus deployment information during development (a JSP page can also be compiled at deployment time). In this way JSP page authoring tools and JSP tag libraries may be used for authoring servlets. The benefits of this approach include:

Compilation of a JSP page in the context of a web application provides resolution of relative URL specifications in include directives and elsewhere, tag library references, and translation-time actions used in custom actions.

A JSP page can also be compiled at deployment time.

JSP class loading.

JSP servlet is loaded.

Creating instance of JSP class.

JSP servlet instance is created.

The enforcement of the contract between the JSP container and the JSP page author is aided by the requirement that the Servlet class corresponding to the JSP page must implement the javax.servlet.jsp.HttpJspPage interface (or the javax.servlet.jsp.JspPage interface if the protocol is not HTTP).

package javax.servlet.jsp;

public interface JspPage extends javax.servlet.Servlet {

	public void jspInit();
	public void jspDestroy();
	public void _jspService(ServletRequestSubtype request,
		ServletResponseSubtype response)
		throws ServletException, IOException; 
		// _jspService - depends on the specific protocol used and 
		// cannot be expressed in a generic way in Java.
}
					
package javax.servlet.jsp;

public interface HttpJspPage extends JspPage {

	public void _jspService(javax.servlet.http.HttpServletRequest req,
		javax.servlet.http.HttpServletResponse res)
		throws javax.servlet.ServletException, java.io.IOException;

}
					

The jspInit method.

The jspInit method, if present, will be called to prepare the page BEFORE the first request is delivered.


<%!
	public void jspInit() {
		...
	}
%>

					

Method is optionally defined in JSP page. Method is invoked when the JSP page is initialized. When method is called all the methods in servlet, including getServletConfig are available.

The _jspService method.

The _jspService(...) method corresponds to the body of the JSP page. This method is defined automatically by the JSP container and should NEVER be defined by the JSP page author.

The formal types of the request/response parameters:

void _jspService(javax.servlet.ServletRequest req,
		javax.servlet.ServletResponse res)
		throws IOException, ServletException {
	...
}
					

The request and response interfaces together describe a protocol-dependent contract between the JSP container and the class that implements the JSP page. The HTTP contract is defined by the javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse interfaces:

void _jspService(javax.servlet.http.HttpServletRequest req,
		javax.servlet.http.HttpServletResponse res)
		throws IOException, ServletException {
	...
}
					

Method MAY NOT be defined in JSP page. The JSP container automatically generates this method, based on the contents of the JSP page. Method invoked at each client request.

The jspDestroy method.

The JSP container can reclaim resources used by a JSP page when a request is not being serviced by the JSP page by invoking its jspDestroy method, if present.


<%!
	public void jspDestroy() {
		...
	}
%>

					

Method is optionally defined in JSP page. Method is invoked BEFORE destroying the page.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide