Mapping a specific servlet to be the default servlet in Tomcat

This should be useful to you.

From the Java™ Servlet Specification Version 3.1 (JSR 340)

Chapter 12. Mapping Requests to Servlets

12.2 Specification of Mappings

In the Web application deployment descriptor, the following syntax is used to define mappings:

  • A string beginning with a / character and ending with a /* suffix is used for
    path mapping.

  • A string beginning with a *. prefix is used as an extension mapping.

  • The empty string ("") is a special URL pattern that exactly maps to the
    application’s context root
    , i.e., requests of the form http://host:port/<contextroot>/.
    In this case the path info is / and the servlet path and context path is
    empty string ("").

  • A string containing only the / character indicates the “default” servlet of the
    application. In this case the servlet path is the request URI minus the context path
    and the path info is null.

  • All other strings are used for exact matches only.

As an addition, read this nice explanation with short examples from the book Head First Servlets & JSP: Passing the Sun Certified Web Component Developer Exam (2nd edition) (quote):

The THREE types of <url-pattern> elements

1) EXACT match

Example:
<url-pattern>/Beer/SelectBeer.do</url-pattern>

  • MUST begin with a slash (/).
  • Can have an extension (like .do), but it’s not required.

2) DIRECTORY match

Example:
<url-pattern>/Beer/*</url-pattern>

  • MUST begin with a slash (/).
  • Always ends with a slash/asterisk (/*).

3) EXTENSION match

Example:
<url-pattern>*.do</url-pattern>

  • MUST begin with an asterisk (*) (NEVER with a slash).
  • After the asterisk, it MUST have a dot extension (.do, .jsp, etc.).

IMPORTANT NOTE:
The URL patterns represent logical / virtual structure, i.e. the patterns (paths) specified does not need to exist physically.


UPDATE

If you want, as you say in your comment,

I want host:port to hit my servlet, not the default tomcat servlet

then see the solution here:
How do I make my web application be the Tomcat default application

In other words, what you want is a path without application context, which implies the application context of the Tomcat default application.

Quote from the above link:

In a standard Tomcat installation, you will notice that under the same
directory (CATALINA_BASE)/webapps/, there is a directory called ROOT
(the capitals are important, even under Windows). That is the
residence of the current Tomcat default application, the one that is
called right now when a user calls up
http://myhost.company.com[:port]
. The trick is to put your
application in its place.

Leave a Comment