Thursday, June 19, 2008

What's JSP? Life Cycle of JSP (JavaServer Pages)

JSP (JavaServer Pages) - what is it?

JSP is a technology which enables the generation of dynamic web contents for a typical web application by separating the user interface generation from the actual content generation. This separation helps the page designers and content developers to do their respective tasks without interfering with each other. JSP can also be extended by adding Custom Tag Libraries to it.

A JSP page is just like a HTML web page that contains (strictly speaking we should say 'that may contain' as a plain static HTML page can also be saved as with a .jsp extension to make it a JSP page, but that's seldom used) additional pieces of code responsible for dynamic content generation. This additional code can utilize many Java APIs including JavaBeans, JDBC, EJB, and RMI for building the dynamic content. HTML part of the JSP page provides the layout in which the dynamically generated content is displayed.

Life Cycle of JSP (JavaServer Pages)

A JSP page is first converted into a Servlet and then that Servlet gets compiled and executed like any other Servlet. The translation of a JSP page into the corresponding Servlet is done by the JSP Engine of the underlying Web Container. The entire life cycle of a typical JSP page can be summarized as the following phases:-

  • Translation - In this phase the JSP page is translated into the corresponding Servlet. This translated Servlet is different from a normal Servlet only in the sense that it implements the interface javax.servlet.jsp.HttpJspPage. Well... if you look at this interface closely, you'll easily notice that a translated JSP page implements this interface just to have the JSP specific features. This HttpJspPage is just a wrapper of the Servlet interface as HttpJspPage interface extends the JspPage interafce and the JspPage interface extends the javax.servlet.Servlet interface. Thus, we see that the translated Servlet also internally implements the javax.servlet.Servlet interface (otherwise we would not have called it a servlet). In addition it implements few more interfaces to support the JSP technology.
  • Compilation - Once the JSP page has been translated into the corresponding Servlet, the next obvious step is to compile that Servlet.
  • Loading & Instantiation - As is the case with any compiled class (.class file), this servlet class also needs to be loaded into the memory before being used. The default classloader of the Web Conatiner will loads this class. Once the class is loaded, an instance of this class gets created. JspPage interaface contains the jspInit() method, which is used by the JSP Engine to initialize the newly created instance. This jspInit() method is just like the init() method of the Servlet and it's called only once during the entire life cycle of a JSP/Servlet.
  • Servicing Requests - _jspService() is the method which is called every time the JSP is requested to serve a request. This method normally executes in a separate thread of execution (provided we have selected Single Thread Model for the JSP) and the main JSP thread keeps waiting for other incoming requests. Every time a request arrives, the main JSP thread spawns a new thread and passes the request (incoming request) and response (new) objects to the _jspService() method which gets executed in the newly spawned thread.
  • Unloading - jspDestroy() method is called (almost always by the Web Container... Read a related article on why a JSP/Servlet writer shouln't call this method explicitly - Calling destroy() from init() in a Servlet >>) before the Web Container unloads the JSP instance. We normally keep code responsible for resource-reclaimation or clean-up activities in this method. After the execution of the jspDestroy() method, the JSP instance is marked for the garbage collection and the occupied memory is eventually reclaimed.



Jineesh said...

Simple and good post

aminosäuren said...

When a request is mapped to a JSP page, it is handled by a special servlet that first checks whether the JSP page's servlet is older than the JSP page. If it is, it translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.