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

谁说我不能喝 提交于 2019-12-06 02:01:14

问题


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!


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/7448316/how-to-exclude-redirect-certain-url-pattern-in-web-xml-or-guice-servlet-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!