1 模板一
package ${enclosing_package};
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ${primary_type_name} implements Filter{
public ${primary_type_name}(){
super();
}
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException{
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain){
try{
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession();
// 这里放到达目的地前(进入)处理代码
filterChain.doFilter(req, res);
// 这里放到达目的地(离开)的处理代码
} catch (IOException e){
e.printStackTrace();
} catch (ServletException e){
e.printStackTrace();
}
}
public void destroy(){
}
}
2 模板二
package ${enclosing_package};
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ${primary_type_name} implements Filter {
public ${primary_type_name}() {
super();
}
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession();
// 这里放到达目的地前(进入)处理代码
filterChain.doFilter(req, res);
// 这里放到达目的地(离开)的处理代码
}
public void destroy() {
}
}