java doFilter 没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3659683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
doFilter not getting called
提问by e.b.white
Could you help to check why doFilter not getting called
你能帮忙看看为什么 doFilter 没有被调用吗
web.xml:
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<filter>
<filter-name>roseFilter</filter-name>
<filter-class>net.paoding.rose.RoseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>roseFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
</web-app>
class signature:
班级签名:
import org.springframework.web.filter.GenericFilterBean;
public class RoseFilter extends GenericFilterBean {
404 is returned while call http://localhost:8080/hello/world, I set the breakpoints at doFilter, it seems doFilter not called?(I tried tomcat 6.0.18, 6.0.29, jdk1.6)
调用http://localhost:8080/hello/world时返回404 ,我在doFilter处设置了断点,似乎没有调用doFilter?(我试过tomcat 6.0.18, 6.0.29, jdk1.6)
回答by BalusC
The filter won't be invoked when:
在以下情况下不会调用过滤器:
The filter class is missing in the classpath and/or is not loadable or instantiable. You should however have noticed it in the server's startup logs. Solution is to be found based on the interpretation of the exceptions/errors found in the server logs.
There's another filter running before in the chain which isn't calling
FilterChain#doFilter()
, but ratherRequestDispatcher#forward()
orinclude()
which caused the subsequent filters in the chain being completely skipped (when they do not listen onFORWARD
orINCLUDE
dispatchers; they by default only listens onREQUEST
dispatcher). Solution is either to fix the wrong filter, or to add<dispatcher>FORWARD</dispatcher>
etc accordingly, or to rearrange the filter declarations inweb.xml
so that your new filter comes beforethe another filter (you in turn only need to ensure that your new filter is using theFilterChain#doFilter()
properly :) ).The request URL is plain wrong. You used http://localhost:8080/hello/world. With a filter listening on
/*
, this means that the webapp context should be ROOT or at least/hello
. Verify your webapp context. I'd just retry with an URL which points to a valid JSP/Servlet inside the same webapp which generates a non-404 response. Does the filter then get called as well?
类路径中缺少过滤器类和/或不可加载或实例化。但是,您应该已经在服务器的启动日志中注意到它。解决方案是根据对服务器日志中发现的异常/错误的解释来找到的。
有一个在未调用链之前运行的另一个过滤器
FilterChain#doFilter()
,而是RequestDispatcher#forward()
或include()
造成后续的过滤器链被完全跳过(当他们不听上FORWARD
或INCLUDE
调度员,他们在默认情况下只侦听REQUEST
调度员)。解决方案是修复错误的过滤器,或相应地添加<dispatcher>FORWARD</dispatcher>
等,或重新排列过滤器声明,web.xml
以便您的新过滤器位于另一个过滤器之前(反过来您只需要确保您的新过滤器FilterChain#doFilter()
正确使用:) )。请求 URL 完全错误。您使用了http://localhost:8080/hello/world。使用过滤器侦听
/*
,这意味着 webapp 上下文应该是 ROOT 或至少是/hello
. 验证您的 webapp 上下文。我只是用一个 URL 重试,该 URL 指向同一个 web 应用程序中的有效 JSP/Servlet,它生成一个非 404 响应。然后过滤器也会被调用吗?
回答by CoolBeans
What's the web request look like? Can you try changing your url-pattern to *.jspinstead of / * ? If you are using something other than pure JSP then change it to whatever the request ending extension is (like for struts it is usually *.do).
网络请求是什么样的?您可以尝试将 url-pattern 更改为*.jsp而不是 /* 吗?如果您使用的不是纯 JSP,那么将它更改为请求结束扩展名的任何内容(例如对于 struts,它通常是 *.do)。