Who is First Filter or Servlet

If we will define both Filter and Servlet mapping in web.xml with same URL pattern. If we call the URL which one is called First. Servlet or Filter.

Thanx sanjay

Eat JAVA, Drink JAVA, Sleep Java

Narendra Dhande

Ranch Hand

Posts: 951

posted 11 years ago

Filters. Mapping purpose is differnt for filter and servlets.

Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0

John Wolf

Ranch Hand

Posts: 64

posted 11 years ago

To confirm, it is the chain.doFilter(request, response) in the Filter above, that calls the servlet with the same url-mapping? Right?

What I am little confused on is ... if filters have matching url-pattern but do not have a servlet-name mapped and there is no servlet with a matching url-mapping, then do filters run on their own with no servlet being called at all.

Sanjay pts

Ranch Hand

Posts: 357

posted 11 years ago

Hi sumit

Think ,that whats the use of filter and servlet .Then the dought will be claer that FILTER will always be called first b'se its Intercepting request and response .

Thanx Sanjay

Eat JAVA, Drink JAVA, Sleep Java

Narendra Dhande

Ranch Hand

Posts: 951

posted 11 years ago

Hi,

Your Quote

What I am little confused on is ... if filters have matching url-pattern but do not have a servlet-name mapped and there is no servlet with a matching url-mapping, then do filters run on their own with no servlet being called at all.

I think the container first find the matching Servlet. If the matching servlet is found then only it execute the filters that matched the given URL before executing the Servlet. The Filters can terminate the processing in between, if required. If no Servlet mapping is found, no filter will run even though the filters have matching URL.

Thanks

Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0

Narendra Dhande

Ranch Hand

Posts: 951

posted 11 years ago

Hi,

Also, The code after doFilter(..) method of FilterChain method executed after the Servlet.