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

3.5. Invoking a Servlet Without a web.xml Mapping

Problem

You want to request a servlet that does not have a servlet-mapping element in the web.xml deployment descriptor.

Solution

Use an invoker-style URL of the form http://www.mysite.org/mywebapp/servlet/com.jspservletcookbook.MyServlet.

Discussion

Some servlets may not have a path mapping in the web application's deployment descriptor. So how can a user request this servlet? What name and URL do they use?

Tomcat and other servlet containers provide a method for invoking servlets that are not mapped in web.xml. You can use a URL of the following form:

 http://www.mysite.org/mywebapp/servlet/<fully qualified class name of servlet>

A servlet with the class and package name of jspservletcookbook.MyServlet is invoked as http://www.mysite.org/mywebapp/servlet/jspservletcookbook.MyServlet. Ensure that the path segment following the name of your web application is /servlet/ and not /servlets/. If the servlet is stored in the default web application (generally at the top level of the servlet container), the URL for invoking it is http:// www.mysite.org/servlet/jspservletcookbook.MyServlet.

The web.xml file located in <Tomcat_install_directory>/conf includes this definition and mapping for the invoker servlet:

<servlet> <servlet-name>invoker</servlet-name> <servlet-class>org.apache.catalina.servlets.InvokerServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <load-on-startup>2</load-on-startup> ...

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