Shammer's Philosophy

My private adversaria

web.xml config template for Restful Application

The web.xml file included war archive of Java WebApp tends to be bigger with a lot of URIs if there are a lot of functions like Restful Web Service Applications. The maintenance of such applications is very mistakable. The debugging URI mismatches also tend to be confused, so less web.xml URI mapping is better.

Fortunately, web.xml URI mapping can be accepted with wildcard. Using wildcard in URI mapping enables Servlet to handle a lot of URI patterns with the getServletPath() which returns the URI gotten rid of the first strings split with /. For example, getServletPath() can return the /aaa/bbb if HTTP Request URI is /servlet/aaa/bbb with configuring web.xml URI pattern as like below.

<servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

The combination of using wildcard url-pattern and getServletPath() method of HttpServletRequest enables only one Servlet to handle a lot of URIs.