Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 585
user_1357 Avatar asked Sep 16 '11 17:09

user_1357


People also ask

How to exclude URL pattern servlet filter?

Modify doFilter() in order to check if the requested URL belongs to the list of predefined excluded URLs, if so then just forward the request to the next filter or servlet in the chain, otherwise do your authentication logic.

How do you exclude a filter in Java?

By default, filters doesn't support excluding a specific URL pattern, whenever you define a URL pattern for a filter then any request matching this pattern is handled by the filter without exceptions. The simplest way for excluding URLs from a filter is to map your filter to a very specific pattern.

What is URL pattern in filter mapping?

The url-pattern element of a servlet-mapping or a filter-mapping associates a filter or servlet with a set of URLs. When a request arrives, the container uses a simple procedure for matching the URL in the request with a url-pattern in the web. xml file.


1 Answers

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.

like image 55
Justin Morgan Avatar answered Oct 05 '22 13:10

Justin Morgan