简体   繁体   English

Java:从servlet到servlet转发时进行过滤

[英]Java: Filter when forwarding from servlet to servlet

is it possible to use a filter when forwarding a request from one servlet to another? 将请求从一个servlet转发到另一个servlet时是否可以使用过滤器? In the web.xml of my project, I've tried this filter-mapping: 在我的项目的web.xml中,我尝试了以下过滤器映射:

<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. 从servlet转发到JSP可以正常工作。 Forwarding from servlet to servlet doesn't call the filter. 从servlet到servlet的转发不会调用过滤器。 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. 它在jsp的情况下起作用,因为请求转到客户端,然后又返回到服务器,但是在servlet的情况下,它没有重定向到客户端,因此您的过滤器不会在那里出现。

If you want then use sendRedirect instead of forward . 如果需要,请使用sendRedirect而不是forward。

Check below link to see difference b/w sendRedirect and forward. 检查以下链接以查看黑白sendRedirect和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. 当您使用SendRedirect容器时,会将请求传输到客户端或浏览器,因此sendRedirect方法中给出的url作为对客户端的新请求可见。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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