struts2-filter
- web.xml配置
- filter类
- 需实现java.io.IOException类
- destroy,init,doFilter的优先级
- web.xml配置
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.yjy.filter.textFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>名字</filter-name>
<filter-class>自定义的class的路径</filter-class>
</filter>
<filter-mapping>
<filter-name>名字</filter-name>
<url-pattern>匹配类型的请求 ,/* 代表全部</url-pattern>
</filter-mapping>
- 需实现javax.servlet.Filter;类
package com.yjy.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class textFilter implements Filter {
@Override
public void destroy() {
System.out.println("进入destroy");
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("进入 doFilter");
//放行
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("进入init");
}
}
- destroy,init,doFilter的优先级、
这个最好自己去测试一下
- init 在服务器启动时就执行
- doFilter 在获取请求前执行
- destroy在服务器销毁时执行