API for the generated servlet

The Container generates a class from your JSP that implements the HttpJspPage interface. This is the only part of the generated servlet’s API that you need to know. You don’t care that in Tomcat, for example, your generated servlet extends:

org.apache.jasper.runtime.HttpJspBase

All you need to know about are the three key methods:

image with no caption
  • jspInit()

    This method is called from the init() method. You can override this method. (Can you figure out how?)

  • jspDestroy

    This method is called from the servlet’s destroy() method. You can override this method as well.

  • _jspService

    This method is called from the servlet’s service() method, which means it runs in a separate thread for each request. The Container passes the Request and Response objects to this method.

    You can’t override this method! You can’t do ANYTHING with this method yourself (except write code that goes inside it), and it’s up to the Container vendor to take your JSP code and fashion the _jspService() method that uses it.

Note

Note the underscore at the front of the _jspService() method

It’s NOT in front of the other two methods, jspInit() and jspDestroy(). Think of it this way, the underscore in front of the method means “don’t touch!”

So, no underscore in front of the name means you can override. But if there IS an underscore in front of the method name, you must NOT try to override it!

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.