Link to home
Start Free TrialLog in
Avatar of skatc2
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?
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

   Hi!

In the web.xml file you just have to specify the correct filter mapping
Example :  
<filter>
    <filter-name>Authentication Filter</filter-name>
    <filter-class>AuthenticationFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>Authentication Filter</filter-name>
    <url-pattern>/index.jsp</url-pattern>
  </filter-mapping>

This example binds an authentication filter to the /index.jsp page

  <filter-mapping>
    <filter-name>Authentication Filter</filter-name>
    <url-pattern>/protected/</url-pattern>
  </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>Authentication Filter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>
the authentication filter is bound to the entire site and is triggered on every single request.

Hope this helps.
Regards,
  Tomas Helgi
Avatar of skatc2
skatc2

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>Authentication Filter</filter-name>
    <url-pattern>/sub/</url-pattern>
  </filter-mapping>

So the natural url-pattern for binding to "domain.com/" should be

  <filter-mapping>
    <filter-name>Authentication Filter</filter-name>
    <url-pattern>/</url-pattern>
  </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.
I thought of a hack.  Go ahead and map to "/"   and in your doFilter method you could have somthing like  
if("index.xhtml".equals(request.getServletPath()){
                                                                                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(request.getServletPath()){
Avatar of skatc2

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).
Avatar of skatc2

ASKER

I happened to come across a simpler answer; glad I had one more look.  This url-pattern:

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

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 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.
ASKER CERTIFIED SOLUTION
Avatar of Venabili
Venabili
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial