You are on page 1of 3

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

d.
Lt
t.
Pv
e
ar
ftw
So
ga
lin

1) INSTANTIATION
When a web container receives a jsp request (may be first or subsequent), it checks for the jsp’s servlet
Ka

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).
1.2) Compilation:
The generated servlet is compiled to validate the syntax. As it is a java class, the compilation is done
using javac command. This will generate the byte code to be run on JVM.
1.3) Loading:
The compiled byte code is loaded by the class loader used by web container. This is a standard process
of using any java class.
1.4) Instantiation:
In this step, instance of the servlet class is created so that it can serve the request.

1.5) Initialization:
Initialization is done by calling the jspInit() method. This is one time activity at the start of the

d.
initialization process. Initialization will make the ServletContext and ServletConfig objects available. One

Lt
can access many attributes related to the web container and the servlet itself. After initialization the
servlet is ready to process requests.

t.
Pv
2) REQUEST PROCESSING
Entire initialization process is done to make the servlet available in order to process the incoming
request. jspService() is the method that actually processes the request. It prints the response in html
(any other) format, using ‘out’ object.
e
ar
3) DESTROY
ftw

Whenever the server is shutting down or when the server needs memory, the server removes the
instance of the servlet. The destroy method jspDestroy() can be called by the server after initialization
So

and before or after request processing. Once destroyed the jsp needs to be initialized again.
Just to summarize, web container handles incoming requests to a jsp by converting it into a servlet and
then by using this servlet to generate the response. Also when the server shuts down, the container
ga

needs to clear the instances.


lin

usebean & Its Scopes in JSP


Ka

“usebean” tag is used to declare and instantiate the JavaBean class in a JSP.

Scope of a JSP object determines whether that is object is available to be used at a particular place in
the application or not. There are four types of scopes available for JSP objects. These are:-

Page Scope

Objects with page scope are accessible only within the page in which they're created. The data is valid
only during the processing of the current response; once the response is sent back to the browser,
the data is no longer valid. If the request is forwarded to another page or the browser makes another
request as a result of a redirect, the data is also lost.
Request Scope

Objects with request scope are accessible from pages processing the same request in which they were
created. Once the container has processed the request, the data is released. Even if the request is
forwarded to another page, the data is still available though not if a redirect is required.

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. A session is the time users spend using the
application, which ends when they close their browser, when they go to another Web site, or when
the application designer wants (after a logout, for instance). So, for example, when users log in, their
username could be stored in the session and displayed on every page they access. This data lasts

d.
until they leave the Web site or log out.

Lt
Application Scope

Objects with application scope are accessible from JSP pages that reside in the same application. This

t.
creates a global object that's available to all pages.

Pv
Application scope uses a single namespace, 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). Application scope variables are typically created and
e
populated when an application starts and then used as read-only for the rest of the application.
ar
ftw
So
ga
lin
Ka

You might also like