How do I designate a servlet to serve as a default servlet for a given webapp under Servlet 2.2 API?
Created May 4, 2012
Avi Kak Through the servlet-mapping element of the webapp deployment
descriptor file web.xml. The url-pattern sub-element of the
servlet-mapping element must be set to '/'.
If made available, a default servlet is executed by the server whenever the client browser points to the URL
http://foo.com/mywebapp/or to the URL
http://foo.com/mywebapp/nonExistentServletFor example, for a webapp named test-suite, the deployment descriptor shown below permits my server to execute the DefaultHelloServlet for the following URLs:
http://localhost:8080/test-suite/ http://localhost:8080/test-suite/NonExistentServletfor any pathname substituted for NonExistentServlet provided it does not begin with /servlet/.
Here is the deployment descriptor (web.xml) for this example that sits in the WEB-INF directory of my test-suite webapp:
<?xml version="1.0"?> <!DOCTYPE web-app SYSTEM +"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> <web-app> <servlet> <servlet-name> hello </servlet-name> <servlet-class> DefaultHelloServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> hello </servlet-name> <url-pattern> / </url-pattern> </servlet-mapping> </web-app>See also Alex Chaffee's Servlet FAQ entry dealing with servlet aliasing.