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
Loading editor…

Generated Servlet Location

The generated .java file is available at:

Example02
Loading editor…

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
Loading editor…

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
Loading editor…

Note on init()

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

Example07
Loading editor…

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
Loading editor…

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
Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.