Life Cycle of JSP – Introduction, Phases and Methods
Expert-led Courses: Transform Your Career – Enroll Now
This article deals with the life cycle of JSP. From its translation to destruction, each and every path followed by the JSP has been discussed in detail. Prior to the discussion of the Life cycle, a basic difference between the web server, container, and application server has been discussed. Various stages and methods of JSP have also been mentioned.
Life Cycle of JSP
As the term suggests, the Life cycle can be defined as the process from creation to destruction along with the series of changes. The same applies to the life cycle of the Java Server pages. It can be defined as the procedure of origination till destruction, including all the phases a JSP follows. Further, we will discuss-
- Difference between Web Server, Web Container and Application Server
- Different stages in the life of JSP
- Discussion of each stage in detail.
Before starting off with the life cycle, it is of utmost importance that we know the difference between a web server, web container and an application server.
Difference between a Web Server, a Web Container, and an Application Server
Web server: It receives HTTP request, intercepts it. It also processes the HTTP response to the web browser of the client. E.g., Apache web server.
Web container: A web container is J2EE compliant implementation. Its main job is to run the runtime environment to JSP and servlets. Request receiver at web browser is forwarded here, and the result generated is sent back to the web server. E.g., Tomcat.
Application Server: It is regarded as the complete server as it provides the facility of both a web server as well as a web container. It is a combination of both formers. E.g., Oracle Application Server, Bea WebLogic.
Stages of Java Server Page
JSP passes through the following phases:
1) Translation of JSP page
2) Compilation of JSP page
3) Class loading
4) Instantiation
5) Initialization
6) Request Processing
7) Destruction
Let’s understand each and every phase in detail with the methods that are invoked:
1) JSP Translation
When the client sends the request to the web server, it goes to the web container. Now evidently, if the JSP page is newer and the servlet class is older, it means that the page got modified. In that case, the translation is done otherwise, this step is skipped so as to improve the performance of the system as it takes time.
2) JSP Compilation
Prior to compilation, JSP engine probes if the page has ever been compiled. If the page has some modifications or if it has never been compiled, then JSP engine compiles the page. Now the compilation follows three steps:
a) Parsing JSP
b) Conversion of JSP to Servlet
c) Compiling the servlet
3) Class Loading
At this stage, the .class file gets loaded by the loader i.e., corresponding servlet class file is loaded.
4) Instantiation
As we compiled the servlet, the object of the generated servlet is created. This process takes place after the class file is loaded in the memory. As soon as the instantiation takes place the objects: ServletConfig and ServletContext get accessible to the JSP class.
ServletConfig is a pre-defined interface used to develop flexible servlets. ServletContext is generated by the web container when the project is deployed. ServletConfig object exists as one per servlet program, whereas ServletContext object exists as one per web application.
5) Initialization
After instantiation, container invokes jspInit() method better known as initialization of JSP. It basically initializes the instance we created. Talking in detail, it is done to initialize resources needed to process the request i.e. resources like databases, allow JSP pages to read data, create lookup tables, and manage network connections.
Though it is invoked by the container only once, it can be overridden by the author.
public void jspInit() { / /Initialization code } /
6) Request Processing
After the initialization of servlet instances, a new thread gets created. All the interactions necessary to process requests occur at this stage. Thus, container invokes _jspService() method. This method contains two parameters:
- HTTPServletRequest req
- HTTPServletResponse res
public void _jspService(HTTPServletRequest req, HTTPServletResponse res) { // Enter jsp Service code here }
This service method is responsible for generating a response to the request. Not only this, but it also generates response to all seven HTTP methods, namely:
a) GET
GET is a read only request. It tends to get information from the server. GET request posses 3 standard triggers:
- Typing in the address line of the browser and clicking enter or go.
- Pressing submit button in HTML form.
- Clicking on the link of a web page in order to access it.
b) POST
This method gets triggered when the form settings is made as a post.
c) HEAD
This method is almost similar to the GET method. Though this method doesn’t return a message body, it can check if a resource hasn’t been recently updated, valid, and accessible.
d) PUT
Using the request payload, PUT method replaces current representations of the target resource.
e) DELETE
This method deletes the specified resource.
f) CONNECT
This method establishes a path identified by the target resource to reach the server.
g) OPTIONS
This method describes the communication options that we need to communicate with the target resource.
7) Destruction
The destruction basically represents cleanup. For the cleanup jspDestroy() method gets invoked. This cleanup means that the JSP page after processing gets removed from the container. This method destroys the instance of the servlet class code, and all the connections to database and network are released. Opened files are also closed. jspDestroy() is overridden whenever the cleanup needs to be done.
public void jspDestroy() { //Cleanup code }
Conclusion
Finally we have seen JSP Life cycle, various phases of JSP Lifecycle and methods invoked in it: In short,
- Web container translates JSP code into servlet class source (.java)
- It is compiled into java servlet class.
- Class Loader loads Servlet class bytecode.
- Container creates instance of that servlet class
- Initialize servlet can now service requests.
- _jspService() is invoked by web container for each request.
- To remove the servlet instance jspDestroy() is called. It performs cleanup.
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google