How to Use Regular Expressions in Web.Xml Url Patterns

Using regular expressions for filter-mapping

No. As to wildcard matching, the <url-pattern> only supports prefix (/folder/*) or suffix (*.extension) matching.

If you want more finer grained matching like as possible with Apache HTTPD's mod_rewrite, then use Tuckey's UrlRewriteFilter or OCPSoft Rewrite instead. They support mod_rewrite-like expressions by XML configuration.

Any way of mapping java servlet using simple regex?

You would need a filter.

This one is a basic one that should work.

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;

String url = req.getRequestURI();
if (url.endsWith("test")) {
request.getServletContext().getNamedDispatcher("TestServlet").forward(request, response);
} else {
chain.doFilter(request, response);
}
}

Key points. I use getNamedDispatcher to get the named servlet you wish to use. It's probably best to not actually map this servlet to a pattern at all.

By using the getNamedDispatcher, it does not corrupt the request url within the destination servlet proper.

What is the meaning of the leading slash / in Tomcat's url-pattern?

Presumably, you mean the <url-pattern> for filter mappings in web.xml. You might not know this, but web.xml is defined as part of the Java Servlet Specification. You can find full documentation for the 3.1 version (currently, the latest and greatest) here:

https://jcp.org/aboutJava/communityprocess/final/jsr340/index.html

If you read section 12.2 ("Specification of Mappings") you can see exactly what kinds of patterns are recognized, including the prefix-mapping you are requesting above.

EDIT 2014-11-04:
You should know that CATALINA_HOME/conf/web.xml (or CATALINA_BASE/conf/web.xml if you have one) is the default configuration for all web applications deployed on Tomcat and that your webapp's WEB-INF/web.xml is the configuration specific for your own web application. Both of these files should have the following xmlns statements and therefore indicate to you (by their URIs) that they are covered by the Java Servlet Specification (or at least Java EE, which includes the servlet spec):

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

Match the request URL to the correct URL pattern in web.xml

the url pattern specification :

1 - A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.

2 - A string beginning with a ‘*.’ prefix is used as an extension mapping.

3 - 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.

4 - All other strings are used for exact matches only.

reference : Java Servlet Specification

Trouble with url-pattern in web.xml

The specifications (paragraph 12.2) says the following:

In the Web application deployment descriptor, the following syntax is
used to define mappings:

  • 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.

  • The
    empty string ("") is a special URL pattern that exactly maps to the
    application's context root, i.e., requests of the form
    http://host:port/<contextroot>/. In this case the path info is ’/’ and
    the servlet path and context path is empty string (““).

  • 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.

So the * is taken literally, unless the pattern ends with /* or starts with *.

Defining servlet url patterns using regular expressions on Google App Engine platform

You can't. The servlet url mapping support is very limited. You can:

  • use UrlRewriteFilter
  • use spring-mvc (or another action web framework) to define controller method patterns
  • provide the parsing logic yourself, if it's simple and it is just an individual case

Servlet url pattern with wildcard in the middle

I'm think you try to solve wrong task. It's really unusual decision to map servlet on wildcard like this. Take a look on Spring MVC framework there you can write methods like this

@RequestMapping("/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)
public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
Owner owner = ownerService.findOwner(ownderId);
Pet pet = owner.getPet(petId);
model.addAttribute("pet", pet);
return "displayPet";
}


Related Topics



Leave a reply



Submit