O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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!

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required