Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use regular expressions in web.xml URL patterns?

I am writing a filter to do a specific task but I am unable to set a specific url pattern to my filter. My filter mapping is as follows:

 <web.xml>   <filter>      <filter-name>myFilter</filter-name>      <filter-class>test.MyFilter</filter-class>    </filter>    <filter-mapping>     <filter-name>myFilter</filter-name>      <url-pattern>/org/test/*/keys/*</url-pattern>    </filter-mapping>  </web-app> 

My url-pattern [ /org/test/ * /keys/ * ] is not working as I had expected.

I am calling urls from the browser like:

http://localhost:8080/myapp/org/test/SuperAdminReport/keys/superAdminReport.jsp http://localhost:8080/myapp/org/test/Adminreport/keys/adminReport.jsp http://localhost:8080/myapp/org/test/OtherReport/keys/otherReport.jsp 

So for the URLs above the filter pattern should match. How can I achieve this?

like image 911
Satya Avatar asked Dec 20 '11 04:12

Satya


People also ask

How do I create a URL pattern in Web XML?

The <servlet-mapping> element specifies a URL pattern and the name of a declared servlet to use for requests whose URL matches the pattern. The URL pattern can use an asterisk ( * ) at the beginning or end of the pattern to indicate zero or more of any character.

What is a regular expression URL?

URL regular expressions can be used to verify if a string has a valid URL format as well as to extract an URL from a string.

What is URL pattern in filter mapping in Web XML?

The url-pattern element of a servlet-mapping or a filter-mapping associates a filter or servlet with a set of URLs. When a request arrives, the container uses a simple procedure for matching the URL in the request with a url-pattern in the web. xml file.


1 Answers

No, you can't use a regex there. According to the Java Servlet Specification v2.4 (section srv.11.1), the url-path is interpreted as follows:

  • 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 con- text path and the path info is null.
  • All other strings are used for exact matches only.

No regexes are allowed. Not even complicated wild-cards.

like image 84
Stephen C Avatar answered Oct 08 '22 13:10

Stephen C