Special Root Level Servlet Paths

In this article I will discuss various root level Servlet paths, such as “/” and “/*”. There is some confusion in this area. Hopefully this article will clear it up.

Wildcard Root “/*”

Set up as:

<servlet-mapping>
<servlet-name>My Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Or:

@WebServlet("/*")

Any URI for the web application will be mapped to this Servlet. Examples (here, “MyWebApp” is the context root of the web application):

  1. /MyWebApp/foo/bar – Will be mapped to the Servlet
  2. /MyWebApp/foo/bar?q=param – Will be mapped to the Servlet
  3. /MyWebApp/foo/bar/file.jsp – Will be mapped to the Servlet. This effectively disables JSP processing.
  4. /MyWebApp/foo/bar/file.png – Will be mapped to the Servlet. This effectively disables file serving from the web application.

Application Root “/”

Set up as:

<servlet-mapping>
<servlet-name>My Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Or:

@WebServlet("/")

All requests will be mapped to this Servlet, except for JSP files. This takes into account welcome JSP file index.jsp. Examples below.

  1. /MyWebApp/ – Will be mapped to the Servlet, unless the /index.jsp file exists. If the JSP file it exists, the request will be handled by the JSP processor.
  2. /MyWebApp/foo/bar – Will be mapped to the Servlet, unless the /foo/bar/index.jsp file exists. If the JSP file exists, the request will be handled by the JSP processor.
  3. /MyWebApp/foo/bar?q=param – Will be mapped to the Servlet, unless the /foo/bar/index.jsp file exists. If the JSP file exists, the request will be handled by the JSP processor.
  4. /MyWebApp/foo/bar/file.png – Will be mapped to the Servlet, even if the file exists under WebContent folder. This effectively disables file serving from the web application.
  5. /MyWebApp/foo/bar/file.jsp – Will not be mapped to the Servlet. If the JSP file exists, it will be processed by the JSP processor. Else, 404 will be returned.

Context Root URI

Set up using an empty URL pattern. This is supported by Servlet 3.0 and later:

<servlet-mapping>
<servlet-name>My Servlet</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>

Or:

@WebServlet("")

Only the root URI for the web application will be mapped to this Servlet. Examples:

  1. /MyWebApp/ – Will be mapped to the Servlet
  2. /MyWebApp/foo/bar – Will not be mapped to the Servlet
  3. /MyWebApp/foo/bar?q=param – Will not be mapped to the Servlet
  4. /MyWebApp/foo/bar/file.jsp – Will not be mapped to the Servlet. The URI will be processed as a normal JSP request.
  5. /MyWebApp/foo/bar/file.png – Will not be mapped to the Servlet. The requested file will be served up if it exists.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s