|
Write a Different Base Class (Continued)
Extending JSP
The solution I decided to use when I came across this problem touched on one of the obscure areas of JSP. When a JSP is first accessed, the Web server compiles the page. The JSP is parsed into Java and the Java is compiled. The compiled JSP is a servlet, and it implements the javax.servlet.Servlet interface, which means the JSP has a service() method that is executed when a request to the page is processed.
The JSP specification defines an interface (javax.jsp.HttpJspPage) that all compiled JSPs must implement. This interface has a method called _jspService() that is implemented by all JSP servlets and is where the JSP does its work. Web servers provide an implementation of this interface and have the servlet that is generated from the JSP extend this interface. In the case of Tomcat 5 the generated Java code looks like this:
public final class
MortgageCalc_jsp
extends org.apache.jasper.
runtime.HttpJspBase
The org.apache.jasper.runtime.HttpJspBase is defined by Tomcat and implements javax.servlet.HttpJspPage. The job of this base class is to implement the servlet interface and to call the JSP's _jspService() method.
The JSP specification allows page authors to define their own base class, which means that we can define a class that implements the service() method that gets called before the page executes. We can then move the Java code that calculates the mortgage to the service method. To get this to work we have to do two things: we need to tell the JSP what class it should extend and we have to write the class. Specifying the class to extend is easy; in the JSP add this line:
<%@ page extends=
"com.develop.MortgageCalcBaseServlet"
%>
Writing the class is only slightly trickier. Our class has to do two things: extend javax.servlet.HttpServlet so that it is an HttpServlet, and implement HttpJspPage so that it is also a JSP servlet. Remember that HttpJspPage has a _jspService() method that will be implemented by the generated servlet; that is, our class does not implement that method and so will be an abstract base class. The class definition will be something like this:
public abstract class
MortgageCalcBaseServlet extends
HttpServlet implements
javax.servlet.jsp.HttpJspPage
{
Now all we have to do is to implement the service() method. In the service() method two things have to happen: the mortgage calculation has to be carried out and the generated servlets _jspService() method must be called, regardless of the result of the calculation. Check out the service() method in Listing 2, which shows the same code that was in the original JSP, but it is now in a servlet. Notice that the call to _jspService() is inside a finally block to ensure that the JSP is always executed.
When you bring this code and the JSP together, the generated servlet looks like this:
public final class
MortgageCalc_jsp
extends com.develop.
MortgageCalcBaseServlet
The generated servlet now extends our base class.
If you want to write simple Web applications that don't need the full power of MVC and you don't want to add code to the page, you have the option instead of writing a Java base class that your JSP can extend and carry out any calculations.
About the Author
Kevin Jones is a developer who has spent the last four years researching and teaching Java programming and most recently investigating HTTP and XML. Kevin lives in the U.K. and works for Developmentor, a training company based in the United States and Europe that specializes in technical training on Java and Microsoft platforms. Contact Kevin at .
Back to top
|