3

My filter mapping in web.xml is as follows:

  <filter>
    <filter-name>LoginCheckFilter</filter-name>
    <filter-class>com.tutorial.filter.LoginCheckFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>LoginCheckFilter</filter-name>
    <url-pattern>/admin*</url-pattern>
  </filter-mapping>

When I run my app and hit http://localhost:8080/admin my filter is not getting executed. I'm not able to understand why.

Is there any problem with the pattern. Also If I remove '*' from the pattern then the filter is getting executed on hitting above url.

Need help on this. Thanks.

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

For future reference, this seems to be servlet spec 3.0. E.g., The empty string ("") does not seem valid for version 2.5.
2

Use /admin/* instead of /admin*.

Servlet container will not recognize /admin* as correct URL pattern.

Comments

1

Maybe try:

<url-pattern>/admin/*</url-pattern>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.