Recipe 5.4 Mapping a JSP to Its Page Implementation Class


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


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.


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 by the servlet-mapping for that JSP page implementation class, the web container will now direct that request to the mapped servlet class.

Example 5-8 shows a servlet configuration for a precompiled JSP.

Example 5-8. A web.xml entry for a precompiled JSP
 <servlet>     <servlet-name>org.apache.jsp.precomp_jsp</servlet-name>     <servlet-class>org.apache.jsp.precomp_jsp</servlet-class> </servlet> <servlet-mapping>     <servlet-name>org.apache.jsp.precomp_jsp</servlet-name>     <url-pattern>/precomp.jsp</url-pattern> </servlet-mapping> 

The directory structure for this class in your web application should be something like: /WEB-INF/classes/org/apache/jsp/precomp_jsp.class . If the context path for your web application is /home , users can request this JSP's implementation class (a servlet, behind the scenes) with a URL similar to http://localhost:8080/home/precomp.jsp .

See Also

Recipe 5.1-Recipe 5.3; Chapter JSP.11.4 of the JSP 2.0 specification.

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: