Question

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!

Était-ce utile?

La solution

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.

Autres conseils

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>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top