Time to see the REAL generated servlet

We’ve been looking at a super-simplified version of the servlet the Container actually creates from your JSP. There’s no need to look at the Container-generated code during development, but you can use it to help learn. Once you’ve seen what the Container does with the different elements of a JSP, you shouldn’t need to ever look at the Container-generated .java source files. Some vendors won’t let you see the generated Java source, and keep only the compiled .class files.

Don’t be intimidated when you see parts of the API that you don’t recognize. Most of the class and interface types are vendor-specific implementations you shouldn’t care about.

What the Container does with your JSP

  • Looks at the directives, for information it might need during translation.

  • Creates an HttpServlet subclass.

    For Tomcat 5, the generated servlet extends:

    org.apache.jasper.runtime.HttpJspBase

  • If there’s a page directive with an import attribute, it writes the import statements at the top of the class file, just below the package statement.

    For Tomcat 5, the package statement (which you don’t care about) is: package org.apache.jsp;

  • If there are declarations, it writes them into the class file, usually just below the class declaration and before the service method. Tomcat 5 declares one static variable and one instance method of its own.

  • Builds the service method. The service method’s actual name is _jspService(). It’s called by the servlet superclass’ overridden service() method, and receives the HttpServletRequest and HttpServletResponse. As part of building this method, the Container declares and initializes all the implicit objects. (You’ll see more implicit objects when you turn the page.)

  • Combines the plain old HTML (called template text), scriptlets, and expressions into the service method, formatting everything and writing it to the PrintWriter response output.

Relax

There’s little on the exam about the generated class.

We’ve been showing generated code so that you can understand how the JSP is translated into servlet code. But you don’t need to know the details about how a particular vendor does it, or what the generated code actually looks like. All you need to know is the behavior of each element type (scriptlet, directive, declaration, etc.) in terms of how that element works inside the generated servlet. You need to know, for example, that your scriptlet can use implicit objects, and you need to know the Servlet API type of the implicit objects. But you do NOT need to know the code used to make those objects available.

The only other thing you need to know about the generated code are the three JSP lifecycle methods: jspInit(), jspDestroy, and _jspService(). (They’re covered later in this chapter.)

Tomcat 5 generated class

<html><body>
<%! int count=0; %>
The page count is now:
<%= ++count %>
</body></html>
image with no caption
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset