Java Servlet & JSP Cookbook
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).
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 .
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 /* .
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. |