Nearby lessons
2 of 30JSP - API (JspPage and HttpJspPage)
- Understand the JSP API interface hierarchy
- Know the lifecycle methods declared by JspPage and HttpJspPage
- Understand how generated servlets extend HttpJspBase
JSP API defines the interfaces that every JSP-generated servlet must implement. This lesson covers the JspPage and HttpJspPage interfaces, their lifecycle methods, and how Tomcat's base class ties them together.
Translation Phase Overview
The process of translating a JSP page (.jsp file) into a corresponding Servlet (.java file) is called the Translation Phase.
For example: demo.jsp becomes demo_jsp.java.
This translation is performed by the JSP Engine. In Tomcat, this component is called JASPER.
Example JSP:
Generated Servlet Location
The generated .java file is available at:
Life Cycle Steps
When a JSP is first requested, the following steps occur in order:
- Translation phase —
.jsp→.java - Compilation phase —
.java→.class - Load the class
- Create an instance
- Call
jspInit()method - Call
_jspService()method - Call
jspDestroy()method
Interface Hierarchy
Every JSP-generated servlet must implement javax.servlet.jsp.JspPage or javax.servlet.jsp.HttpJspPage, either directly or indirectly.
Tomcat provides the base class org.apache.jasper.runtime.HttpJspBase which already implements both interfaces.
I) JspPage Interface
The JspPage interface is in the javax.servlet.jsp package and defines two lifecycle methods:
jspInit() Method
Signature: public void jspInit()
This method is executed only once — at the time of the first request — to perform initialization activities.
The web container always calls init(ServletConfig) of HttpJspBase, which internally calls jspInit().
You can override jspInit() in your JSP to define custom initialization:
Note on init()
We cannot place init(ServletConfig) in the JSP because it is declared as final in HttpJspBase.
jspDestroy() Method
Signature: public void jspDestroy()
This method is executed only once — to perform cleanup activities just before the JSP is taken out of service.
The web container calls destroy() of HttpJspBase, which internally calls our jspDestroy() method.
II) HttpJspPage Interface
The HttpJspPage interface is a child interface of JspPage. It defines only one method:
Signature: public void _jspService(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
This method is executed separately for every request.
The web container calls service() of HttpJspBase, which internally calls our _jspService() method.
Rules for Writing Methods Explicitly
| Question | Answer |
|---|---|
Can we write _jspService() explicitly? | No — it is auto-generated by the JSP Engine. Writing it causes a compile-time error (two identical methods). |
Why does _ appear in _jspService? | It indicates this method is auto-generated and cannot be written explicitly. |
Can we write service() explicitly? | No — it is declared final in HttpJspBase. |
Summary of overridable methods in a JSP:
init()— final in parent class, cannot writejspInit()— can overridedestroy()— final in parent class, cannot writejspDestroy()— can overrideservice()— final in parent class, cannot write_jspService()— auto-generated, cannot write explicitly
- Every generated JSP servlet implements JspPage (directly or indirectly)
- JspPage declares jspInit() and jspDestroy()
- HttpJspPage declares _jspService()