O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

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

5.4. Mapping a JSP to Its Page Implementation Class

Problem

You have already precompiled a JSP and want to specify a mapping to the JSP page implementation class in your deployment descriptor.

Solution

Cut and paste the servlet and servlet-mapping elements generated automatically by JspC into web.xml. Create the proper package-related directories in the WEB-INF/classes directory of your web application, then place the precompiled JSPs into that directory.

Discussion

Precompiling JSPs allows you to remove the JSP page syntax files from your web application and just use the resulting servlet class files. You can then use the servlet-mapping element in web.xml to map a JSP-style URL (e.g., default.jsp) to the compiled servlet class. Here is how to accomplish this task:

  1. Precompile the JSP(s) as described in Recipe 5.1 or Recipe 5.2, including the compilation of Java source files into class files using javac or another compiler tool.

  2. Cut and paste the servlet and servlet-mapping elements generated automatically by JspC into your deployment descriptor (if you are using Tomcat), or add those elements manually to web.xml (if you are using WebLogic or another container).

  3. Make sure the servlet-mapping's url-pattern element points to a JSP-style filename, such as default.jsp, or an extension mapping such as *.jsp.

  4. Place the class or classes, including the package-related directories, in WEB-INF/classes, or inside of a JAR file that is stored in WEB-INF/lib.

When the web users request the URL specified ...

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