Introduction to JSP Life Cycle
In this article, we will discuss the JSP Life Cycle. Java server pages, a server-side technology that takes the responsibility of processing at server.
1. Where do we need JSP?
- Well, if you are into web-based development and what we see is building dynamic websites, then JSPs play a vital role as rendering is done dynamically.
- JSPs work on underlying HTML only, the body of a page is constituted of basic HTML markups only but there are certain placeholders, parameters, list values, iterators those are feasible only with JSPs, HTML gives you static display only while any resolutions to be done at run time needs a dynamic handling , so JSPs appear as a solution there.
- JSP enables separation of content generation and content presentation
Let’s take here a basic example which will give you an idea of this dynamic rendering.
<% out.print(“This is my first JSP page”); %>
The content or logic inside <% %> is rendered dynamically and is passed by back-end only.
Now let’s move ahead and understand the life cycle of JSPs and how do they actually get rendered at the front end to provide a customer readably or ready experience.
2. Life Cycle of JSP :
There are certain phases in which this cycle is divided, defined as follows –
- Translation phase – where the JSP file gets converted into the servlet file.
- Compilation phase – where servlet file gets converted to servlet class.
- Class loading
- Instantiation of the servlet object
- Servlet container calls jspInit() method for initialization
- Servlet container calls the _jspService() for processing the request
- Clean up of the JSP, here servlet container will call jspDestroy() method.
Now lets elaborate the above-mentioned steps to have a clearer picture –
4.5 (2,185 ratings)
In the translation phase, the servlet container translates the JSP to a servlet so that the tag-based coding gets an equivalent form in java so that Java Virtual Machine (JVM) can understand that (as it won’t understand the language of tags). This translation is done by the server either at the time of deploying web application or when JSP gets its first request for execution.
Now the time comes for a compilation of source code, i.e. converting the servlet code into java byte code (java byte code is an instruction set for java virtual machine).
The servlet class that was loaded from the JSP source will now be loaded into the container.
After loading a class file by the web container, JSP container now uses a no-argument constructor for creating an instance of the servlet class. Now once the container initializes the objects by an invocation of jsplnit() method.
Public void jsplnit()
// servlet initialization related snipped to be placed here
Now the time comes for request processing, here the initialized JSP equivalent servlet objects are used for processing the client requests. _jspService() method is invoked by the web container. This invocation is done one the servlet object passing HTTPServletRequest object and HTTPServletResponse object for respective requests and responses. Note that there is no provision to override the _jspService() method.
Public void _jspService( HttpServletRequest request, HttpServletResponse response)
//snipped is placed here
The last step is called JSP clean up, JSP has to be removed from use by the container and the jspDestroy() method is used for the same, this method shall be invoked once only. There is a provision to override this method and that can be done for instances where we want to perform our custom action like connection release for database etc.
public void jspDestory()
// snippet is placed here
Let us create a login form for our sample application and we will be using JSP for certain content rendering here.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My Login Form</title>
Date d = new Date();
System.out.println("Current Date= "+d);
<form action="actionName" method="post">
<table style="with: 50%">
<td><input type="text" name="userName" /></td>
<td> Password </td>
<td><input type="password" name="password" /></td>
In the above example, the code placed inside <% %> is rendered dynamically as we mentioned prior also, now once this request is submitted, it will be received by the action controller which is basically a Java class that contains the parameters corresponding to the names mentioned in the JSP file.
A servlet container creates a modal object and content can be referred to in other JSPs by referring to this particular controller.
This kind of MVC based approach nowadays finds a place with technologies like –
- Spring MVC
- Struts 2
Spring MVC and Struts 2 have introduced their specific URI to use JSPs in a customized manner and they also introduce different varieties of tags for that like autocomplete, iterator, list, etc.
So JSP finds an ideal place in many frameworks due to their flexibility.
- Hence we learned that dynamic websites have their frontend rendered via JSPs, while static pages can be directly put to HTML purely as they do not need any data/content manipulations thenceforth.
- Modern technologies are compatible with JSPs and are used by struts and spring like frameworks that find a place in big applications related to banking, SCM, retail stores, etc.
- Compatible with AJAX calls for dynamic rendering.
- An application cannot be of type single page as every time page is required to be loaded from the backend.
- Tiles can be used for base layout formats in struts and spring and what we need to render again and again is just body.
- Ultimately codes get converted to HTML equivalent at the front end.
- JSPs differentiate the concept of content presentation and content generation.
- Interceptors are provided by various frameworks that make the life easy for the developers like utilities in struts are provided for file upload processes etc.
This has been a guide to JSP Life Cycle. Here we discussed the introduction, steps, and example of the JSP Life Cycle. You can also go through our other suggested articles to learn more –