简体   繁体   中英

Servlet Filter redirect: How to redirect after chain.doFilter(request,resp);

I have a servlet filter class . Inside the doFilter function I would like to redirect to a custom URL after chain.doFilter(request,response) call.

Now I know that I cant call both chain.doFilter(request,response) and response.redirect("www.customURL.com") together. But the idea is that the browser redirects to the customUrl after the filter chain is complete.

How can this be accomplished in the doFilter function of my servlet filter class (which obviously implements the filter class).

Thanks.

Short answer: You can't.

Somewhat longer answer: Reorder your filters. Care for all filters you want to be processed to be added before your redirecting filter. Filters are processed in the order they are added to web.xml .

You can do this if you wrap the response object and implement any calls that explicitly or implicitly commit the response as NO-OPs. What you are essentially doing by doing this is throwing away the response generated by the filter chain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM