In the end, a JSP is just a servlet

Your JSP eventually becomes a full-fledged servlet running in your web app. It’s a lot like any other servlet, except that the servlet class is written for you—by the Container.

The Container takes what you’ve written in your JSP, translates it into a servlet class source (.java) file, then compiles that into a Java servlet class. After that, it’s just servlets all the way down, and the servlet runs in exactly the same way it would if you’d written and compiled the code yourself. In other words, the Container loads the servlet class, instantiates and initializes it, makes a separate thread for each request, and calls the servlet’s service() method.

Note

The most important point for this chapter is simply: what role does your JSP code play in the final servlet class?

In other words, where do the elements in the JSP end up in the source code of the generated servlet?

image with no caption

Some of the questions we’ll answer in this chapter include:

  • Where does each part of your JSP file end up in the servlet source code?

  • Do you have access to the “servletness” of your JSP page?

    For example, does a JSP have a concept of a ServletConfig or ServletContext?

  • What are the types of elements you can put in a JSP?

  • What’s the syntax for the different elements in a JSP?

  • What’s the lifecycle of a JSP, and can you step into the middle of it?

  • How do the different elements in a JSP interact in the ...

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.