当前位置:编程学习 > JAVA >>

doFilter不起作用???

刚写的Filter:
Java code

import javax.servlet.*;
import javax.servlet.http.*;
 
public class LogFilter implements Filter{
     
    FilterConfig filterConfig = null;
     
    public void init(FilterConfig filterConfig) {
    System.out.println("Filter Begin...");
        this.filterConfig = filterConfig;
    }
     
    public void destroy(){
        System.out.println("Filter Des...");
        this.filterConfig = null;
    }
     
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain){
        System.out.println("doFilter");
        try{
             
        ((HttpServletResponse )response).sendRedirect("result.jsp");
            ServletContext sc = filterConfig.getServletContext();
            long before =System.currentTimeMillis();
            System.out.println("getServletPath:"+((HttpServletRequest)request).getServletPath());
            System.out.println("getRequestURI:"+((HttpServletRequest)request).getRequestURI());
            chain.doFilter(request,response);
            long after = System.currentTimeMillis();
            System.out.println(after-before);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}


xml配置:
Plain Text code
<?xml version="1.0" encoding="ISO-8859-1"?>
 
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">
 
     
  <servlet>
      <servlet-name>loginServlet</servlet-name>
      <servlet-class>LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>loginServlet</servlet-name>
      <url-pattern>/login</url-pattern>
  </servlet-mapping>
   
   
  <filter>
      <filter-name>log</filter-name>
      <filter-class>fil.LogFilter</filter-class>
  </filter>
  <fiter-mapping>
      <filter-name>log</filter-name>
      <url-pattern>/*</url-pattern>
  </fiter-mapping>
   
</web-app>



为什么只有init()和destroy()方法执行?而且还是在Applications开启的时候执行。执行jsp或servlet都不过滤? --------------------编程问答-------------------- 配置没发现问题。Servlet 执行了没 --------------------编程问答-------------------- 你的filter是在fil包下吗? --------------------编程问答--------------------
引用 1 楼 defonds 的回复:
配置没发现问题。Servlet 执行了没

Servlet执行了! --------------------编程问答--------------------
引用 2 楼 huxiweng 的回复:
你的filter是在fil包下吗?

在!
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,