How to exclude/redirect certain url pattern in web.xml or Guice servlet module?
Asked Answered
C

2

6

I need to serve my main application with the url pattern "/*" so this pattern is matched to a Servlet. The problem I am having is now all the css files and images located at "/css/all.css", "/images/" etc are going through this Servlet which is undesirable. I want these files to be directly accessed. What is the better way to handle this situation?

Note: I am using Guice's Servlet Module to configure the patterns.

Thanks!

Clifford answered 16/9, 2011 at 17:25 Comment(1)
P
5

We need to know specifically which requests should be routed to your servlet, so that we know how to code the rules. I can't tell whether a) all requests except CSS and images should be sent to your servlet, or b) your servlet should only handle requests to a specific set of folders/directories. You will probably want to do one of two things:

Exclude specific folders:

^/(?!css|images).*

Or include specific folders:

^/myservlet/.*

You should change those * symbols to + if, as you indicated in your earlier question, you want to require at least one character after the / in the pattern.

Pusey answered 16/9, 2011 at 18:52 Comment(2)
Thanks! I was trying to use filter to achieve this but this is more elegant!Clifford
Note that this only applies to Guice, and not the web.xml mentioned in the question title. The web.xml url-pattern only has support for simple path and extension mapping wildcards: /* and .*Ozonize
D
0

This should work for you:

Make all your image/css etc resources go through the default servlet. And make a mapping like this:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.xml</url-pattern>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.png</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
Dissenter answered 21/3, 2014 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.