Nearby lessons

2 of 30

JSP - API (JspPage and HttpJspPage)

📌 What You Will Learn
  • 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:

Example01
JCode Cell
1 
2<h1>The Server time is:<%= new java.util.Date() %></h1>
3

Generated Servlet Location

The generated .java file is available at:

Example02
JCode Cell
1 
2D:\Tomcat 7.0\work\Catalina\localhost\jspdapp1\org\apache\jsp\demo_jsp.java
3

Life Cycle Steps

When a JSP is first requested, the following steps occur in order:

  1. Translation phase — .jsp.java
  2. Compilation phase — .java.class
  3. Load the class
  4. Create an instance
  5. Call jspInit() method
  6. Call _jspService() method
  7. 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.

Example04
JCode Cell
1 
2javax.servlet.Servlet (I)
3 |
4javax.servlet.jsp.JspPage (I)
5 |
6javax.servlet.jsp.HttpJspPage (I)
7 
8HttpServlet (AC) GenericServlet (AC)
9 /
10 HttpJspBase (AC) ← Tomcat's base class
11 |
12 demo_jsp.java (C) ← your generated servlet
13

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:

Example06
JCode Cell
1 
2<%!
3public void jspInit() {
4 System.out.println("jsp initialization activities");
5}
6%>
7<h1>The Server Time is: <%= new java.util.Date() %></h1>
8
Output

jsp initialization activities
      

Note on init()

We cannot place init(ServletConfig) in the JSP because it is declared as final in HttpJspBase.

Example07
JCode Cell
1 
2public abstract class HttpJspBase {
3 public final void init(ServletConfig config) {
4 jspInit();
5 }
6}
7

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.

Example08
JCode Cell
1 
2<%!
3public void jspDestroy() {
4 System.out.println("jsp cleanup activities");
5}
6%>
7<h1>The Server Time is: <%= new java.util.Date() %></h1>
8
Output

jsp cleanup activities
      

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.

Example09
JCode Cell
1 
2public abstract class HttpJspBase extends HttpServlet {
3 public final void service(HttpServletRequest request,
4 HttpServletResponse response)
5 throws ServletException, IOException {
6 _jspService(request, response);
7 }
8}
9

Rules for Writing Methods Explicitly

QuestionAnswer
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 write
  • jspInit()can override
  • destroy()final in parent class, cannot write
  • jspDestroy()can override
  • service()final in parent class, cannot write
  • _jspService()auto-generated, cannot write explicitly
📝 Key Takeaways
  • Every generated JSP servlet implements JspPage (directly or indirectly)
  • JspPage declares jspInit() and jspDestroy()
  • HttpJspPage declares _jspService()

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3