Life Cycle of a JSP

When a web container or servlet container receives a request from client for a jsp page, it takes the jsp through its various life cycle phases, and then returns the response to the client. What all things these containers should support, is defined by the jsp and servlet specifications. The web containers can be a part of web servers, e.g. tomcat, and application servers. Following diagram shows the different life cycle stages of jsp. Broadly, these stages can be classified into three. • Instantiation • Request Processing • Destruction

1) INSTANTIATION When a web container receives a jsp request (may be first or subsequent), it checks for the jsp’s servlet instance. If no servlet instance is available or if it is older than the jsp, then, the web container creates the servlet instance using following stages. • Translation • Compilation • Loading • Instantiation • Initialization 1.1) Translation: Web container translates (converts) the jsp code into a servlet code. This means that jsp is actually a servlet. After this stage, there is no jsp, everything is a servlet. This task will create a complete jsp page, by considering all included components. Here on, the static content and dynamic contents are treated differently. The resultant is a java class instead of an html page (which we wrote).




So ftw







lin “usebean” tag is used to declare and instantiate the JavaBean class in a JSP. This is one time activity at the start of the initialization process. jspService() is the method that actually processes the request.5) Initialization: Initialization is done by calling the jspInit() method. One can access many attributes related to the web container and the servlet itself. 1.2) Compilation: The generated servlet is compiled to validate the syntax. It prints the response in html (any other) format. The destroy method jspDestroy() can be called by the server after initialization and before or after request processing. instance of the servlet class is created so that it can serve the request. There are four types of scopes available for JSP objects. the compilation is done using javac command. Lt d. Scope of a JSP object determines whether that is object is available to be used at a particular place in the application or not. 1. the data is no longer valid. Ka ga usebean & Its Scopes in JSP So ftw 3) DESTROY ar e Entire initialization process is done to make the servlet available in order to process the incoming request. Pv t. Initialization will make the ServletContext and ServletConfig objects available. 2) REQUEST PROCESSING Whenever the server is shutting down or when the server needs memory. The data is valid only during the processing of the current response. This is a standard process of using any java class. Also when the server shuts down.1. If the request is forwarded to another page or the browser makes another request as a result of a redirect. As it is a java class. the server removes the instance of the servlet.4) Instantiation: In this step. web container handles incoming requests to a jsp by converting it into a servlet and then by using this servlet to generate the response. the container needs to clear the instances. After initialization the servlet is ready to process requests. Once destroyed the jsp needs to be initialized again. .3) Loading: The compiled byte code is loaded by the class loader used by web container. Just to summarize. These are:Page Scope Objects with page scope are accessible only within the page in which they're created. the data is also lost. This will generate the byte code to be run on JVM. once the response is sent back to the browser. 1. using ‘out’ object.

the data is released. So. Session Scope Objects with session scope are accessible from pages processing requests that are in the same session as the one in which they were created. . which ends when they close their browser.Request Scope Objects with request scope are accessible from pages processing the same request in which they were created. when they go to another Web site. This data lasts until they leave the Web site or log out. for instance). This creates a global object that's available to all pages. when users log in. for example. Objects with application scope are accessible from JSP pages that reside in the same application. the data is still available though not if a redirect is required. Application Scope Ka lin ga So ftw ar Application scope uses a single namespace. their username could be stored in the session and displayed on every page they access. Application scope variables are typically created and populated when an application starts and then used as read-only for the rest of the application. e Pv t. or when the application designer wants (after a logout. Even if the request is forwarded to another page. Lt d. A session is the time users spend using the application. Once the container has processed the request. which means all your pages should be careful not to duplicate the names of application scope objects or change the values when they're likely to be read by another page (this is called thread safety).

Sign up to vote on this title
UsefulNot useful