简体   繁体   中英

Java: Filter when forwarding from servlet to servlet

is it possible to use a filter when forwarding a request from one servlet to another? In the web.xml of my project, I've tried this filter-mapping:

<filter-mapping>
  <filter-name>myFilter</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

Forwarding from servlet to JSP works fine. Forwarding from servlet to servlet doesn't call the filter. I wonder if I'm doing something wrong or it's just not possible.

Its working in case of jsp because request goes to client and then comes back to server but in case of servlet it is not redirected to client so your filter does not comes into picture there.

If you want then use sendRedirect instead of forward .

Check below link to see difference b/w sendRedirect and forward.

When you use SendRedirect container transfers the request to client or browser so url given inside the sendRedirect method is visible as a new request to the client.

Read more: http://javarevisited.blogspot.com/2011/09/sendredirect-forward-jsp-servlet.html#ixzz4QvHxSVFF

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