Tuesday, July 22, 2014

JSP:The Lifecycle

A JSP page services requests as a servlet. So, the life cycle of JSP are determined by Java Servlet technology.
There are following steps followed by JSP
1.Translation and Compilation
2.Initialization
3.Execution
4.Destruction
Translation and Compilation
When a browser asks for a JSP, the web container first checks to see whether the JSP page's servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.During the translation phase each type of data (Html and JSP elements) in a JSP page is treated differently. Both the translation and the compilation phases can yield errors that are observed only when the page is requested for the first time. If an error is encountered during either phase, the servers will return JasperException and a message that includes the name of the JSP page and the line where the error occurred.
Initialization
After the page has been translated and compiled, container loads JSP page’s servlet class and instantiates an instance of the servlet class by calling jspInit method.
Execution
Whenever a browser requests a JSP and the page has been loaded and initialized, the web container invokes the _jspService () method in the JSP.
The _jspService () method takes an HttpServletRequest and an HttpServletResponse as its parameter. The _jspService () method of a JSP is invoked once per a request and is responsible for generating the response for that request. We can control various JSP page execution parameters by using page directive. When a JSP page is executed, output written to the response object is automatically buffered. We can set the size of the buffer using the following page directive:
<%@ page buffer="none|xxxkb" %>
A larger buffer allows more content to be written before anything is actually sent back to the client, thus providing the JSP page with more time to set appropriate status codes and headers or to forward to another web resource. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.
Destruction
The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container. If the container needs to remove the JSP page's servlet, it calls the jspDestroy method.

No comments:

Copyright © Codingnodes,2014.All Rights Reserved.