skatc2
asked on
Singling out the root document (/) in a web.xml <url-pattern> mapping
I am adding a filter to a web.xml file. I would like it to trigger for the root index file (domain.com/index.xhtml).
However, the way our server redirects to the index page, it will not also trigger for the root directory (domain.com/) unless explicitly told to. As "/" is defined as the default mapping and matches all requests, how do I define a url-pattern mapping to the root directory?
However, the way our server redirects to the index page, it will not also trigger for the root directory (domain.com/) unless explicitly told to. As "/" is defined as the default mapping and matches all requests, how do I define a url-pattern mapping to the root directory?
ASKER
Nope, please read my question again. I know how to bind the filter to "domain.com/index.jsp". I _want_ to bind the filter to "domain.com/". Now, if I wanted to bind the filter to "domain.com/sub/" I would do it like this:
<filter-mapping>
<filter-name>Authenticatio n Filter</filter-name>
<url-pattern>/sub/</url-pa ttern>
</filter-mapping>
So the natural url-pattern for binding to "domain.com/" should be
<filter-mapping>
<filter-name>Authenticatio n Filter</filter-name>
<url-pattern>/</url-patter n>
</filter-mapping>
Except that "/" has been overloaded to mean _all_ files. So how do I bind to only "domain.com/"?
<filter-mapping>
<filter-name>Authenticatio
<url-pattern>/sub/</url-pa
</filter-mapping>
So the natural url-pattern for binding to "domain.com/" should be
<filter-mapping>
<filter-name>Authenticatio
<url-pattern>/</url-patter
</filter-mapping>
Except that "/" has been overloaded to mean _all_ files. So how do I bind to only "domain.com/"?
I don't think you can do it from web.xml. You could put a filter in for your Tomcat server, and redirect if the request is just for domain.com/ .
Or you can put Apache up front, and do more complicated redirection with htaccess-like commands.
Or you can put Apache up front, and do more complicated redirection with htaccess-like commands.
I thought of a hack. Go ahead and map to "/" and in your doFilter method you could have somthing like
if("index.xhtml".equals(re quest.getS ervletPath ()){
filter code and chain
}
If that doesn't work, then you could try the same idea after changing the file name to index.jsp( if that is ok).
if("index.xhtml".equals(re
filter code and chain
}
If that doesn't work, then you could try the same idea after changing the file name to index.jsp( if that is ok).
Yes, if you use a filter in Tomcat you can do it.
I forgot the slash.
if("/index.xhtml".equals(r equest.get ServletPat h()){
if("/index.xhtml".equals(r
ASKER
Thanks for the ideas and the source code to clairfy. I can't quite get away with the if ("/index.xhtml hack because the filter has to work for other pages, but I could send in the page name to trigger on as a filter parameter.
First I'm going to try playing around with Apache, which I think is running up front already, and see if I can get it to redirect in some different manner or at a later time. (or maybe replace the redirection with a filter).
First I'm going to try playing around with Apache, which I think is running up front already, and see if I can get it to redirect in some different manner or at a later time. (or maybe replace the redirection with a filter).
ASKER
I happened to come across a simpler answer; glad I had one more look. This url-pattern:
<url-pattern>//*</url-patt ern>
Is what I was looking for; this maps to the root page (eg domain.com/) and doesn't seem to match anything else. I can't find any documentation for why this pattern works; I just found it mentioned in a post on the Sun forums (http://forum.java.sun.com/thread.jspa?threadID=283180&messageID=3479996).
<url-pattern>//*</url-patt
Is what I was looking for; this maps to the root page (eg domain.com/) and doesn't seem to match anything else. I can't find any documentation for why this pattern works; I just found it mentioned in a post on the Sun forums (http://forum.java.sun.com/thread.jspa?threadID=283180&messageID=3479996).
I am glad you found a solution. You should just keep the points. You did the research.
>I can't find any documentation for why this pattern works;
Neither can I.
>I can't find any documentation for why this pattern works;
Neither can I.
I agree with rrz@871311 -- you found the best solution, you should keep the points.
Totally weird that your solution works. We should keep this question on EE, just not award points.
You can post a request to refund points to the community support list.
Totally weird that your solution works. We should keep this question on EE, just not award points.
You can post a request to refund points to the community support list.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In the web.xml file you just have to specify the correct filter mapping
Example :
<filter>
<filter-name>Authenticatio
<filter-class>Authenticati
</filter>
<filter-mapping>
<filter-name>Authenticatio
<url-pattern>/index.jsp</u
</filter-mapping>
This example binds an authentication filter to the /index.jsp page
<filter-mapping>
<filter-name>Authenticatio
<url-pattern>/protected/</
</filter-mapping>
This example binds an authentication filter to all pages under the directory/path "protected"
and so if you have the filter mapping like this
<filter-mapping>
<filter-name>Authenticatio
<url-pattern>/</url-patter
</filter-mapping>
the authentication filter is bound to the entire site and is triggered on every single request.
Hope this helps.
Regards,
Tomas Helgi