最近学习Filter过程遇到了这个问题,很是纠结研究了两天总算搞明白了,下面我给大家分享下我的经验吧(有点夸张了):
1、当在过滤器执行过程中如果有报错这种情况下,一般好解决按图索骥怎么都能搞定(一般是路径问题),可是没有报错的情况下对新手来说就很头疼了,init()方法可以正常执行就是不执行doPost()或doGet(),这两天我遇到的情况是这样的,可以正常跳转到servlet但是只是执行了init();dopost根本一点反应都没有,好像就直接跳过一样,最后打断点才找到错误在哪里上代码:
这是我的web配置其中并未在<param-value>LogIn.jsp;failure.jsp;LogInServlet</param-value>中加入Servlet路径这是造成无反应的根本原因。
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ImoocFilter</display-name>
<servlet>
<servlet-name>LogInServlet</servlet-name>
<servlet-class>imooc.filter.servlet.LogInServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogInServlet</servlet-name>
<url-pattern>/LogInServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>LogIn</filter-name>
<filter-class>imooc.filter.login.LogIn</filter-class>
<init-param>
<param-name>checkjsp</param-name>
<param-value>LogIn.jsp;failure.jsp;LogInServlet</param-value>
</init-param>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LogIn</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这是Filter代码(请忽略风骚的测试)
public class LogIn implements Filter {
FilterConfig config;
public LogIn() {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("我是快乐的测试");
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
HttpSession session=req.getSession();
String st=config.getInitParameter("checkjsp");
String charset = config.getInitParameter("charset");
if(charset==null){
charset = "UTF-8";
}
request.setCharacterEncoding(charset);
if(st!=null){
System.out.println("我是快乐的测试1");
String[] c=st.split(";");
for(int i=0;i<c.length;i++){
if(c[i]==null||" ".equals(c[i])) continue;
if(req.getRequestURL().indexOf(c[i])!=-1){
chain.doFilter(request, response);
System.out.println(req.getRequestURL()+"结束本方法");
return;
}
}
System.out.println("我是快乐的测试34567");
}
if(session.getAttribute("username")!=null){
System.out.println("我是快乐的测试345");
chain.doFilter(request, response);
}else{
System.out.println(request.getParameter("username"));
System.out.println(session.getAttribute("username"));
System.out.println("我是快乐的测试347");
res.sendRedirect("LogIn.jsp");
}
System.out.println("我是快乐的测试二");
}
public void init(FilterConfig fConfig) throws ServletException {
config=fConfig;
}
}
错误直接原因就在代码中,因为web.xml中没有加入Servlet路径,所以在Filter代码运行中涉及到servlet判断中就不可能执行chain.doFilter();没法回到Servlet中,因此看着就像直接跳过一样, 其实根本就没有执行。希望遇到同样问题的同学少走弯路。