Metawerx Java Hosting Small Logo

<servlet-mapping>

This tag specifies a URL mapping for a servlet that has been defined with the <servlet> tag.

Servlets cannot be called directly unless the InvokerServlet is enabled, so one or more servlet tags and servlet-mappings must exist for each servlet, to tell Tomcat when to call the servlet.

Multiple <servlet-mapping> tags can be specified for a single <servlet>, providing different URL patterns. See the <url-pattern> tag for examples.

There are two required elements:


Order of elements in web.xml

  • Despite some developers stating that the order of elements in web.xml must follow an exact order, for the most part this is incorrect. However, some elements must be defined before others. Servlet and servlet-mapping are one such example.
  • A servlet-mapping must not be defined in web.xml before the servlet entry. For example, the following will not work, and will throw an java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name.
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/somePath/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.mycompany.MyServlet</servlet-class>
    </servlet>
  • Correct version:
    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.mycompany.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/somePath/*</url-pattern>
    </servlet-mapping>
navigation
metawerx specific
search
Share
tools
help

referring pages

Share