doFilter不起作用
刚写的Filter: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配置:
<?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都不过滤? --------------------编程问答-------------------- 第6行
FilterConfig filterConfig = null;
去掉。。
另外在doFilter方法的结尾加上chain.doFilter(request, response); --------------------编程问答--------------------
不好意思,看错了。。
只要把 第6行
FilterConfig filterConfig = null;
去掉。。
doFilter方法你加了,没看到,sorry
--------------------编程问答-------------------- init()初始化时候执行一次。。。
在Web.xml少了一段struts2的filter
<filter>--------------------编程问答--------------------
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
去掉了怎样用FilterConfig 的方法啊? --------------------编程问答--------------------
init()初始化时候执行一次。。。
在Web.xml少了一段struts2的filter<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
需要不用struts2做的! --------------------编程问答-------------------- "doFilter"没打印吗 --------------------编程问答--------------------
"doFilter"没打印吗
对,下面那些也没打出来。
补充:Java , Web 开发