Translation phase

  1. The process of translating .jsp file to the corresponding servlet class(.java file) is called translation phase.
  2. This can be done by the jsp container in tomcat this component is called “JASPER“.

Translation

The generated .java file is available in the following location in tomcat
c:\Tomcat6.0\work\catalina\jsp1\org\apache\jsp

jsp1 is the name of the application.

JSP API

  1. Every Servlet class generated for the jsp must implement javax.Servlet.jsp.JspPage interface either directly or indirectly.
  2. Tomcat people provided a base class org.apache.jasper.runtime.HttpJspBase to implement JspPage and HttpJspPage interfaces,hence every servlet which is generated by tomcat should extend HttpJspBase class

JSPAPI

javax.servlet.jsp.JspPage

This interface defines the following 2 life cycle methods

  1. jspInit()
  2. jspDestroy()
jspInit()
public void jspInit()
  1. we can implement this method to define initialization activities.
  2. jspInit() method will be executed only once.
  3. Web Container always calls init(servletConfig) method present in HttpJspBase class,Which intern calls jspInit() method.
  4. If we have any initialization activities then we can override jspInit() method in the jsp.
<h3>The server time is <%=new java.util.Date()%></h3>
<%!
public void jspInit() {
System.out.printl("own method");
}
%>

We can’t place init(ServletConfig) in the jsp because it is declared as the final in HttpJspBase class.

jspDestroy()
public void jspDestroy()
  1. This method will be executed only once to perform clean-up activities just before taking jsp from out of service.
  2. Web Container always calls destroy() method available in HttpJspBase class which intern calls jspDestory() method.
  3. We can override jspDestroy() method but we can’t override destroy() method in jsp ,because it is declared as the final in HttpJspBase class

HttpJspPage

It is the child interface of jspPage. This interface contains only one method _jspService() method

public void -jspService(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
  1. Web container always generates this method at the time of “translation phase” and we are not responsible to write this method explicitly.
  2. If we are trying to write this method explicitly in the jsp the generated servlet class will contain 2 _jspService() methods which causes a compile time error.(A class cann’t have 2 methods with the same signature otherwise compiletime error.)
  3. Web container always calls service() method of HttpJspBase which intern calls _jspService() method.
  4. In the jsp we can’t write service() method directly,because it is declared as final in base class.