×
☰ See All Chapters

How JSP Works

JSP container has a JSP file compiler called as page compiler. When HTTP requests with URLs that has the .jsp file extension comes to server, the page compiler parses and compiles the .jsp page into a servlet class.

                               how-jsp-works-0
 

To demonstrate How JSP works by converting code to servlet class, let us deploy the below dome.jsp file to tomcat server.

<html>

<body bgcolor="yellow">

        <h1>HELLO JSP</h1>

</body>

</html>

Now navigate inside server directory e.g.: C:\apache-tomcat-7.0.52\, Now do search as “*demo*”, we can get files demo_jsp.class  and  demo_jsp.java  

how-jsp-works-1
 

When you open the demo_ jsp.java file, you will see the following code:

/*

 * Generated by the Jasper component of Apache Tomcat

 * Version: Apache Tomcat/7.0.52

 * Generated at: 2018-07-08 10:40:56 UTC

 * Note: The last modified time of this file was set to

 *       the last modified time of the source file after

 *       generation to assist with modification tracking.

 */

package org.apache.jsp.jsp;

 

import javax.servlet.*;

import javax.servlet.http.*;

import javax.servlet.jsp.*;

 

public final class demo_jsp extends org.apache.jasper.runtime.HttpJspBase

                implements org.apache.jasper.runtime.JspSourceDependent {

 

        private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory

                        .getDefaultFactory();

 

        private static java.util.Map<java.lang.String, java.lang.Long> _jspx_dependants;

 

        private javax.el.ExpressionFactory _el_expressionfactory;

        private org.apache.tomcat.InstanceManager _jsp_instancemanager;

 

        public java.util.Map<java.lang.String, java.lang.Long> getDependants() {

                return _jspx_dependants;

        }

 

        public void _jspInit() {

                _el_expressionfactory = _jspxFactory.getJspApplicationContext(

                                getServletConfig().getServletContext()).getExpressionFactory();

                _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory

                                .getInstanceManager(getServletConfig());

        }

 

        public void _jspDestroy() {

        }

 

        public void _jspService(

                        final javax.servlet.http.HttpServletRequest request,

                        final javax.servlet.http.HttpServletResponse response)

                        throws java.io.IOException, javax.servlet.ServletException {

 

                final javax.servlet.jsp.PageContext pageContext;

                javax.servlet.http.HttpSession session = null;

                final javax.servlet.ServletContext application;

                final javax.servlet.ServletConfig config;

                javax.servlet.jsp.JspWriter out = null;

                final java.lang.Object page = this;

                javax.servlet.jsp.JspWriter _jspx_out = null;

                javax.servlet.jsp.PageContext _jspx_page_context = null;

 

                try {

                        response.setContentType("text/html");

                        pageContext = _jspxFactory.getPageContext(this, request, response,

                                        null, true, 8192, true);

                        _jspx_page_context = pageContext;

                        application = pageContext.getServletContext();

                        config = pageContext.getServletConfig();

                        session = pageContext.getSession();

                        out = pageContext.getOut();

                        _jspx_out = out;

 

                        out.write("<html>\r\n");

                        out.write("<body bgcolor=\"yellow\">\r\n");

                        out.write("\t<h1>HELLO JSP</h1>\r\n");

                        out.write("</body>\r\n");

                        out.write("</html>");

                } catch (java.lang.Throwable t) {

                        if (!(t instanceof javax.servlet.jsp.SkipPageException)) {

                                out = _jspx_out;

                                if (out != null && out.getBufferSize() != 0)

                                        try {

                                                out.clearBuffer();

                                        } catch (java.io.IOException e) {

                                        }

                                if (_jspx_page_context != null)

                                        _jspx_page_context.handlePageException(t);

                                else

                                        throw new ServletException(t);

                        }

                } finally {

                        _jspxFactory.releasePageContext(_jspx_page_context);

                }

        }

}

 

 

So JSP file will be first converted to servlets and response will be given by these servlets.

Obviously one may think that the very first request for a .jsp page will take much time to produce response due to the time spent for compiling the .jsp file into a jsp servlet. To overcome this, JSP container will pre-compile and keep it ready before any user request for them.

Having examined the generated JSP servlet source code, there are 3 life cycle methods to support a JSP:

  • jspInit() 

  • _jspService() 

  • jspDestroy() 

Life cycle stages in JSP work flow

Below are the different life cycle stages of JSP starting from the request till response given back to user.

  1. Conversion:  When server identifies the HTTP request for a JSP page, server forwards the request to JSP engine. The JSP engine loads the JSP page from disk and converts it into servlet content. Scriptlets (<%  %>) will be converted to java statements and will be kept inside  _jspService() method. Expressions (<%= "Statement" %>) will be converted to println( ) statements. All the declarations (<%! declaration statements %>) will be converted to class level members and will be placed directly inside the servlet class outside the jspService() method.   This stage of converting jsp code to servlet code happens only once. 

  2. Compilation:  The above created servlet code will be compiled to .class file. This stages executes happens only once. 

  3. Instantiation: Object of above servlet class will be created. This stage executes only once and hence only one servlet object creation for each JSP file. 

  4. Initialization:  jspInit() method executes and does any initialization needed. This stage executes only once.  

  5. Service:  _jspService() method executes. This happens for every request. During execution, the servlet produces an output in HTML format. The output is further passed on to the web server by the servlet engine inside an HTTP response. The web server forwards the HTTP response to client browser in terms of static HTML content. 

  6. Finalization: When server is shutdown, jspDestroy() method executes and does any cleanup operation. This happens only once. 

  7. Destruction: Servlet instance will be destroyed when server is shutdown. Until server is up and running all the servlet instances will be kept alive.  

 

how-jsp-works-2
 

All Chapters
Author