The JSP lifecycle is similar to a servlet life cycle but it includes an additional step for the compilation of the JSP page into servlet along with the creation till destruction of a page of JSP. When we save a JSP page it is saved with an extension that is .jsp, through this extension the server is able to know that this page is a JSP page and it needs translation and it also has to go through the stages of lifecycle. First the JSP page has to be converted to servlets and then servlets needs to be processed by the server.
The lifecycle of JSP has four major steps:
The lifecycle of JSP basically is the translation of the JSP page into servlet. The above steps are the basic steps that are involved in the lifecycle of JSP. The following are the steps in detail for the lifecycle of JSP:
- Class Loading
- Request processing
In the first step that is translation, the JSP page is translated to servlet. In the next step that is compilation the servlet code is compiled and now it is a byte code. In loading the class file is loaded into the class file loader or we can say that the servlet class is loaded. In instantiation, the servlet instance is created or in other words the object of the generated servlet is created. In initialization, the method jspInit () is called by the container. Then comes request processing, in this step the jspService () method is called by the container. In next step that is destroy the jspDestroy () method is called by the container.
Consider the following figure in which the lifecycle of JSP is shown:
Let’s suppose that we have a JSP page “abc”. At first it has an extension of .jsp that is abc.jsp. The first step is translation, when abc.jsp is translated its extension will be .java that is it is translated into servlet class source file (.java), then this code in the next step will be compiled and now it is in byte code that is into the java servlet class. Now next step is loading in which the class file is loaded or servlet class file is loaded. The web container after this creates the instance of the servlet class by invoking jspInit () method. Then container calls jspService () method for processing each of the request of the client and in the end jspDestory () method is called by the container to remove the servlet instance from service and to perform cleanup.
The code that is written inside <% %> is the JSP code. Consider the following example in which it is demonstrated that what will happen when we convert the JSP code into servlet:
< html >
< head >
< title >JSP Page< /title >
< /head >
int n = 1;
Value of n:
<% out.println (++n); %>
This JSP page will be converted to the following servlet:
public class abc_jsp extends HttpServlet
public void _jspService (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
PrintWriter out = response.getWriter ();
response. setContenType (“text/ html”);
out.write (“< html>< body >”);
int n = 1;
out.write (“Value of n”);
out.print (++ n);
out.write (“< /body >< /html >”);
The servlet is converted automatically by the web container and therefore, you do not have to worry that how the JSP page is converted to servlet.