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!

user_1357
  • 7,766
  • 13
  • 63
  • 106

2 Answers2

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.

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
  • Thanks! I was trying to use filter to achieve this but this is more elegant! – user_1357 Sep 16 '11 at 20:15
  • 2
    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 `.*` – kapex Oct 01 '14 at 14:35
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>
Jens
  • 1,599
  • 14
  • 33