john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

servlet url mapping

PRACTICAL

(a file named NAME.war will be "exploded" or deployed as )

/var/lib/tomcat6/webapps/NAME

Inside is /var/lib/tomcat6/webapps/NAME/WEB-INF/web.xml

Using a browser verify: http://webserver.com/NAME/SERVLETNAME

HACK: RENAME TO REDUCE THE URL
mv /var/lib/tomcat6/webapps/NAME /var/lib/tomcat6/webapps/ROOT



THEORY

Servlets use web.xml to determine when the application container (i.e. tomcat) should
direct traffic to the Application (i.e. Helloworld)


1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.

2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the '/' character as a path separator. The longest match determines the servlet selected.

3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last '.' character.

4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used.




Here's an eclipse generated web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javae$
  <display-name>HelloServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Hi</display-name>
    <servlet-name>Hi</servlet-name>
    <servlet-class>Hi</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hi</servlet-name>
    <url-pattern>/Hi</url-pattern>
  </servlet-mapping>
</web-app>


SIMPLIFIED web.xml to get http://webserver.com/hi

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javae$
  <display-name>HelloServlet</display-name>
  <servlet>
    <display-name>Hi</display-name>
    <servlet-name>Hi</servlet-name>
    <servlet-class>Hi</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hi</servlet-name>
    <url-pattern>/hi</url-pattern>
  </servlet-mapping>
</web-app>

  • « InstallCert modified
  • deploy webapp to ROOT »

Published

Mar 26, 2012

Category

java-servlet

~255 words

Tags

  • java-servlet 61
  • mapping 1
  • servlet 17
  • url 14