Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use regular expression for Jetty's servlet-mapping?

I have this one mapping

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

but i also want /service/master to map to master servlet.

<servlet-mapping>
<servlet-name>master</servlet-name>
<url-pattern>/service/master</url-pattern>
</servlet-mapping>

I believe there is a conflict here since calling /service/* will trigger service servlet right away. Is there a way for me to use some kind of exclusion in servlet-mapping or may be regexp to do what I want to do?

like image 597
denniss Avatar asked Jan 25 '11 19:01

denniss


2 Answers

Servlet mappings always use the most specific match, so the path <context>/service/master will always map to master.

This is the 1st rule of mappings from the Servlet 3.0 spec:

  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. Many containers provide an implicit default servlet for serving content.
like image 116
McDowell Avatar answered Oct 19 '22 09:10

McDowell


You can try using Google Guice. com.google.inject.servlet.ServletModule.serveRegex(String regex, String... regexes) will let you use regex in mapping.

see here http://code.google.com/p/google-guice/wiki/ServletModule

like image 38
Joel Handwell Avatar answered Oct 19 '22 11:10

Joel Handwell