File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Url Matching Algorithm Used By Tomcat for web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Url Matching Algorithm Used By Tomcat for web.xml" Watch "Url Matching Algorithm Used By Tomcat for web.xml" New topic

Url Matching Algorithm Used By Tomcat for web.xml

Jarryd Folino

Joined: Apr 28, 2011
Posts: 2
I am trying to set up a simple java program that can do the following:

* Takes in a ' given url' such as '/places/australia'
* Takes in a list of URL mappings such as ["*.do", *jsp, "/places", "/places/aust"]

I want to do this to investigate exactly which given URL's will match against the servlet and filter mappings given in a web.xml file. My initial guess was to use the standard Java Pattern and Matcher classes as per below:

However when I ran the code I got the following exception:

This would imply that the standard java regex classes are not used for matching the URL's in web.xml.

Does anyone know of the class library tomcat or a similar container uses to determine if a given url matches the web.xml mappings?
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

The matching algorithm is specified in the servlet specification and isn't (or shouldn't be) Tomcat specific. I would seriously doubt that it uses regex libs for a couple of reasons; one, the pattern matching is pretty simple and regex would be overkill, two, the URL matching predates the regex libs. There are some rules about what kind of matching takes precedence that could not be expressed in regex syntax also.

If memory serves correct, there are four kinds of mapping; path, extension, default and exact.
A string beginning with a / character and ending with a /* suffix is used for path mapping.
A string beginning with a * prefix is used as an extension mapping.
A string containing only the / character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
All other strings are used for exact matches only.

Exact match takes precedence over path and extension.
Path match takes precedence over extension.
Default servlet matches anything else.
I agree. Here's the link:
subject: Url Matching Algorithm Used By Tomcat for web.xml
jQuery in Action, 3rd edition