805 OCPJP 6 Questions 788 SCJP 5 questions 650 OCPJWCD 5 questions 600 OCAJP 7 questions 610 OCPJP 7 questions 510 Upgrade to OCPJP 7 questions 900 Java J2EE Qns


SCWCD : JSP page life cycle

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:

  • Implements HttpJspPage if the protocol is HTTP, or JspPage otherwise.

  • All of the methods in the Servlet interface are declared final.

Additionally, it is the responsibility of the JSP page author that the provided superclass satisfies:
  • The service method of the servlet API invokes the _jspService method.

  • The init(ServletConfig) method stores the configuration, makes it available via getServletConfig, then invokes jspInit.

  • The destroy method invokes jspDestroy.

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:

  • Removal of the start-up lag that occurs when a container must translate a JSP page upon receipt of the first request.

  • Reduction of the footprint needed to run a JSP container, as the Java compiler is not needed.

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.

scjp 1.5 | scjp 1.6 | scwcd 1.5
Java Certifications