Java – servlet filter URL mapping

How do I map a filter to the root of a URL? I'm using Tomcat 7.0 2 and deploy the application as root war. The welcome page is sign_ in. xhtml. Whenever the client sends a request to the site root directory (i.e. domain name only), or the client requests a sign_ in. When XHTML, I want to run the filter This is what I have done so far:

<filter>
        <filter-name>My filter</filter-name>
        <filter-class>com.myApp.myFilter</filter-class>        
    </filter>
    <filter-mapping>
        <filter-name>My filter</filter-name>
        <url-pattern>/sign_in.xhtml</url-pattern>
    </filter-mapping>

Direct request sign_ in. XHTML, successfully called the filter, but I don't know how to get the request of root user to call the filter According to servlet specification (version 3.0)

<url-pattern>/</url-pattern>

Map to the default servlet and empty string to the root This is the relevant part of the specification:

"An empty string (" ") is a special URL pattern that completely maps to the context root of the application, the form http://host:port/ /Your request In this case, the path information is' / ', and the servlet path and context path are empty strings (' ') “

However, both of the following URL patterns cause Tomcat to throw an exception

<url-pattern></url-pattern>
<url-pattern>""</url-pattern>

I would appreciate it if anyone could know something about it thank you.

Andrew

Solution

< URL pattern > / < / url pattern > should be applicable to the request of root user Have you tried?

If your intention is to filter all requests, you should use < URL pattern > / * < / url pattern >

Update: in order to exclude one of them, I tested the URL pattern of / on Tomcat 7 (using web.xml and @ webfilter (urlpatterns = {"/})), which does not work as expected But the expected effect of / url pattern on Tomcat 6 (servlet 2.5) and GlassFish V3 (servlet 3.0) I suspect there is an error in Tomcat 7, so I have reported issue 49914

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>