Creating a Servlet Mapping

In order to make your web app portable, you can map the servlet to a different URL so that relative references in HTML and JSP work.

Edit the web.xml for the web app with the following sets of tags.

Servlet

Place the following with other Servlet tags, or after the Description tag.

<servlet>
  <servlet-name>NewName</servlet-name>
  <servlet-class>package.path.RealName</servlet-class>
</servlet>

NewName can be any name that you like.
package.path.RealName must be the actual package and file name of the servlet

Servlet-mapping

Place the following with other servlet-mappings or after the last servlet element.

<servlet-mapping>
  <servlet-name>NewName</servlet-name> <url-pattern>/AnyNameAtAll</url-pattern> </servlet-mapping>

NewName must match the servlet-name in the servlet tag.

Warning

You must be very careful about where you put these tags in the web.xml file. If they are in the wrong order, then your application will not start.

For more info on the web.xml file you can view the DTD for it: http://java.sun.com/dtd/web-app_2_3.dtd

This is the order of all elements in the web.xml file. The only required element is web-app. If an element is followed by a ?, then it can only appear once. If the element is followed by a *, then it can appear multiple times, but all grouped together.

web-app (icon?, display-name?, description?, distributable?,
context-param*, filter*, filter-mapping*, listener*, servlet*,
servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?,
error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,
login-config?, security-role*, env-entry*, ejb-ref*,  ejb-local-ref*)