Recipe 3.6 Mapping All Requests Within a Web Application to a Servlet


Recipe 3.6 Mapping All Requests Within a Web Application to a Servlet

Problem

You want to have all web application requests go to a single controller servlet.

Solution

Use a servlet-mapping element in your deployment descriptor, with a url-pattern element of <url-pattern>/*</url-pattern> .

Discussion

In some cases, you might want to have all requests related to the web application to go a single servlet. This servlet controller may log requests, implement security, or examine and optionally alter the request object before it forwards the request to another location (usually another servlet or JSP).

For the Sun Microsystems description of the Front Controller design pattern, which is a method for using a servlet as a central processing point, see the Core J2EE Blueprints page at http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html.


Once again, web.xml is the place to configure a servlet to receive all web application requests. Example 3-8 shows how to use a URL pattern to aim all requests at a controller servlet.

Example 3-8. Aiming all requests at a controller servlet
 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"            "http://java.sun.com/dtd/web-application_2_3.dtd" > <web-app>  <servlet>         <servlet-name>Interceptor</servlet-name>         <servlet-class>com.jspservletcookbook.Interceptor</servlet-class>     </servlet>     <!-- The mappings for the Interceptor servlet -->     <servlet-mapping>         <servlet-name>Interceptor</servlet-name>         <url-pattern>/*</url-pattern>     </servlet-mapping>     <servlet-mapping>         <servlet-name>Interceptor</servlet-name>         <url-pattern>/servlet/*</url-pattern>     </servlet-mapping>  </web-app> 

You may also have to override any default invoker servlet with your own mapping:

 <url-pattern>/servlet/*</url-pattern> 

Map the servlet that you want to receive all web application requests to this URL pattern as well. If you keep the invoker servlet the way it is, users could bypass the controller servlet by using a URL like http://www.mysite.org/myapp/servlet/com.jspservletcookbook.CookieServlet .

In Tomcat, you can also disable the invoker servlet in the top-level web.xml file (in <Tomcat_install_directory>/conf ) by commenting out the servlet-mapping element. This affects all other web applications running under that Tomcat instance, however, so this decision should be made collectively among administrators who deploy applications on that server.


You must also remove, alter, or comment out other servlet-mapping elements that allow servlet requests to bypass the controller servlet. If a more specific mapping (such as the one in Example 3-9) is included in web.xml , requests for the CookieServlet will bypass the Interceptor servlet.

Example 3-9. Specific mappings override mappings using wildcard symbols
 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"            "http://java.sun.com/dtd/web-application_2_3.dtd" > <web-app>           <servlet>               <servlet-name>Interceptor</servlet-name>               <servlet-class>jspservletcookbook.Interceptor</servlet-class>             </servlet>  <servlet>                    <servlet-name>CookieServlet</servlet-name>                    <servlet-class>              com.jspservletcookbook.CookieServlet            </servlet-class>           </servlet>  <servlet-mapping>                 <servlet-name>Interceptor</servlet-name>                 <url-pattern>/*</url-pattern>           </servlet-mapping>  <servlet-mapping>               <servlet-name>CookieServlet</servlet-name>               <url-pattern>/CookieServlet</url-pattern>            </servlet-mapping>  </web-app> 

The servlet-mapping element for CookieServlet in this example would cause the servlet path of /CookieServlet to bypass the Interceptor servlet, because the servlet path of /CookieServlet (as part of a request that looks like http://host:port/context-path/CookieServlet ) is a more exact match to the URL pattern of /CookieServlet than it is to /* .

The requests for static content such as welcome files (e.g., index.html ) are also intercepted by the URL pattern /* . The requests for these static files will also go to the controller servlet.


See Also

Chapter 1 on web.xml ; Recipe 3.1-Recipe 3.4; Recipe 3.6-Recipe 3.8; Chapter 11 of the Servlet v2.3 and 2.4 specifications on mapping requests to servlets; the Core J2EE Blueprints page: http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html.



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

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net