Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip a filter in the filter chain in java

I have 2 filters in my application. Based on some condition, I want to choose whether to execute the second filter or not. Is there a way to do this?

I did some googling with no success. I want the request to continue without executing the second filter. Is that possible?

Any help will be appreciated.

like image 260
Vanchinathan Chandrasekaran Avatar asked Sep 17 '10 19:09

Vanchinathan Chandrasekaran


People also ask

How do you skip a filter in Java?

In addition to Colin's answer, there's another way: just don't call FilterChain#doFilter() , but RequestDispatcher#forward() . But this will skip all filters from current on, expect of the ones which are listening on <dispatcher>FORWARD</dispatcher> . Good solution.

What is doFilter () method in Java?

The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

What is filter chain in servlet?

A FilterChain is an object provided by the servlet container to the developer giving a view into the invocation chain of a filtered request for a resource.

What is filter and interceptor?

Filters and interceptors can be used on both sides, on the client and the server side. Filters can modify inbound and outbound requests and responses including modification of headers, entity and other request/response parameters. Interceptors are used primarily for modification of entity input and output streams.


1 Answers

In addition to Colin's answer, there's another way: just don't call FilterChain#doFilter(), but RequestDispatcher#forward().

if (condition) {     request.getRequestDispatcher(((HttpServletRequest) request).getServletPath()).forward(request, response); } else {     chain.doFilter(request, response); } 

But this will skip all filters from current on, expect of the ones which are listening on <dispatcher>FORWARD</dispatcher>.

like image 198
BalusC Avatar answered Sep 20 '22 02:09

BalusC