What REALLY happens to your JSP code?

You write a JSP, but it becomes a servlet. The only way to really tell what’s happening is to look at what the Container does to your JSP code. In other words, how does the Container translate your JSP into a servlet?

Once you know where different JSP elements land in the servlet’s class file, you’ll find it much easier to know how to structure your JSP.

The servlet code on this page is not the real code generated by the Container—we simplified it down to the essential parts. The Container-generated servlet file is, well, uglier. The real generated servlet source code is slightly harder to read, but we will look at the real thing in a few pages. For now, though, all we care about is where in the servlet class our JSP code actually ends up.

image with no caption

Note

ALL scriptlet and expression code lands in a service method.

That means variables declared in a scriptlet are always LOCAL variables!

Note

Note: if you want to see the generated servlet code from Tomcat, look in yourTomcatHomeDir/work/Catalina/yourServerName/yourWebAppName/org/apache/jsp. (The underlined names will change depending on your system and your web app.)

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.