0% found this document useful (0 votes)
487 views4 pages

JSP Architecture and Lifecycle of JSP (Advanced Java)

Uploaded by

sekhon85578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
487 views4 pages

JSP Architecture and Lifecycle of JSP (Advanced Java)

Uploaded by

sekhon85578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JSP Architecture

JSP stands for Java Server Pages. JSP Processing is a fast way to create dynamic web
content. It allows rapid development of web-based applications that are server and platform-
independent. It enables the insertion of java code into HTML pages by making special JSP
tags. It consists of either XML, HTML with JSP actions and commands. Information can be
shared across pages using response and request objects. JSP can also be used to
access JavaBeans objects.

we will understand the JSP Processing.

What is JSP Architecture?


JSP (JavaServer Pages) architecture is a framework for building web applications in Java. It
has three main parts: a web container, a JSP engine, and a servlet container.
The JSP engine processes requests from the user and generates dynamic content using Java
code and JSP tags. The servlet container manages the lifecycle of servlets and JSP pages. JSP
architecture follows the Model-View-Controller (MVC) pattern, where the Model contains
the application's business logic and data, the View displays the data to the user, and the
Controller manages the interaction between the Model and the View. Overall, JSP
architecture provides a scalable and efficient way to build complex web applications in Java.
JSP architecture is a three-tier architecture consisting of a web server, client, and database.

Web Server
It uses a JSP engine like a container that processes JSP. E.g., Apache Tomcat. This engine
intercepts the request for JSP and provides a runtime environment for the understanding of
JSP processing files. It reads, parses, builds Java Servlet, Compiles, Executes Java code, and
returns the HTML page to the client.

Client
It is the web browser/application on the user’s side.

Database
It is used to store the user’s data. The web server has access to the database.

JSP Architecture Flow


1. The client navigates to the file ending with the .jsp, and the browser initiates an HTTP
request to the webserver.

2. If the compiled version of the JSP file exists in the web server, it returns the file.
Otherwise, the request is forwarded to the JSP engine. If the modification date on
the JSP file is older than its generated servlet, then the JSP container assumes that the
JSP hasn't changed and the generated servlet still matches the JSP's contents. This makes
the process more efficient and faster than other scripting languages.

3. The JSP engine loads the JSP file and translates the JSP to Servlet. This is done by
converting all the template text into println() statements and JSP elements to Java code.
This process is called translation.

4. The JSP engine compiles the servlet to an executable .class file. It is then forwarded to
the servlet engine. This process is known as the compilation/request processing phase.

5. The .class file is executed by the servlet engine, which generates the output as an HTML
file. The servlet engine further passes the output as an HTTP response to the webserver.

6. At last, the webserver forwards the HTML file to the client’s browser. The browser
handles the dynamically generated HTML page exactly like a static page.

Lifecycle of JSP
A JSP lifecycle can be defined as the process that starts with its creation which is further
translated into a servlet, and then the servlet life cycle comes into play.
Steps Involved in a JSP lifecycle:
 Translation
 Compilation
 ClassLoading
 Instantiation
 Initialization
 Request Processing
 Destroy
Translation
This phase deals with the syntactic correctness of the page. JSP code is checked by the JSP
container and is parsed by it to generate the servlet source code. The web container
automatically translates the index.jsp file into index.java.

Compilation
Here, the JSP container compiles the JSP class source code. It converts the index.java file
into an index.class file. The translation of the Java source to its implementation class can
occur at any given time between the deployment of the JSP page into the container and
processing of the JSP page.

ClassLoading
In this phase, the servlet class loaded from the JSP source is loaded into the container.

Instantiation
In this phase, an instance of the class is generated. The container manages one or more
instances by responding to the requests.

Initialization
In this phase, the container invokes the init method of the JSP class. After that, servlet
configuration with the init parameter is configured. Now, JSP can handle the client requests.
Most often, the translation and initialization in JSP happen as soon as the first request for a
JSP comes.

Request Processing
This is the most time-consuming phase of the JSP. Here, the JSP page processes the requests
from a client. The request processing phase is multi-threaded. A new thread is used for every
request, and a servlet request and response object are created. After this, the _jspService()
method is called. This method cannot be overridden.

Destroy
The last phase of the JSP lifecycle is destroyed. The JSP class is unloaded or removed from
the container in this phase. It is done when the application is undeployed or when the server
is down. This is done using the jspDestroy() method and can be overridden.
JSP Lifecycle Methods

jspInit()
This is declared on the JSP page. This method is called only once in the complete lifecycle of
JSP. This is used to initialize configuration params in a deployment descriptor. This method
can be overridden using a JSP declaration scripting element.
_jspService()
It is invoked by the JSP container for each client request. This method passes the request and
response objects. It cannot be overridden, and hence, it starts with an underscore. It is defined
and declared in the HttpJspPage interface.
jspDestroy()
This is used for shutting down the application/container. This method is called only once in
the complete JSP lifecycle when JSP is unloaded from the memory. This method should be
overridden only to release resources created in the JSP init method.

You might also like