How to exclude / redirect a specific url template in web.xml or in the Guice servlet module?

I need to serve the main application with the url pattern "/*" so that this pattern matches the servlet. The problem I am facing now is all the css files and images located in "/css/all.css", "/ images /" etc. Pass through this servlet, which is undesirable. I want these files to be directly accessible. What is the best way to handle this situation?

Note. I use the Guice Servlet Module to customize the templates.

Thanks!

+6
source share
2 answers

We need to know what requests should be sent to your servlet so that we know how to encode the rules. I can’t say whether all requests, except CSS and images, should be sent to your servlet or b) your servlet should only process requests for a specific set of folders / directories. You probably want to do one of two things:

Exclude specific folders:

 ^/(?!css|images).* 

Or include specific folders:

 ^/myservlet/.* 

You should change these * characters to + if, as you pointed out in your previous question, you want to require at least one character after / in the pattern.

+5
source

This should work for you:

Make all your image / css resources etc. through the default servlet . And do the mapping as follows:

 <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> 
0
source

Source: https://habr.com/ru/post/897481/


All Articles