(转)Servlet3.0下@WebFilter注解配置Filter

Servlet3.0下@WebFilter注解配置Filter

Filter(过滤器)主要对请求到达前进行处理,也可以在请求结束后进行处理,类似于链式。一个请求可以被多个过滤器拦截到,会依次进入各个Filter中,放行后直至进入Servlet,Servlet处理请求结束后,回到各个Filter继续执行后面的代码,先执行的Filter后执行完。

常用到的地方:

  • 用户权限过滤
  • 记录日志
  • 字符编码处理

配置Filter:

  • @WebFilter注解
  • web.xml中配置
@WebFilter常用属性

属性 类型 是否必需 说明
asyncSupported boolean 指定Filter是否支持异步模式
dispatcherTypes DispatcherType[] 指定Filter对哪种方式的请求进行过滤。
支持的属性:ASYNC、ERROR、FORWARD、INCLUDE、REQUEST;
默认过滤所有方式的请求
filterName String Filter名称
initParams WebInitParam[] 配置参数
displayName String Filter显示名
servletNames String[] 指定对哪些Servlet进行过滤
urlPatterns/value String[] 两个属性作用相同,指定拦截的路径

用户权限过滤示例:

1.方式一,@WebFilter注解方式

自定义过滤器,实现javax.servlet.Filter接口,通过注解方式配置。拦截所有的请求,放行登录页面、登录操作请求,其余请求需要在登录后才可访问。同时配置参数,指定要放行的路径和请求的字符集。

[java] view plain copy
  1. @WebFilter(filterName = "loginFilter",   
  2.     urlPatterns = "/*",   
  3.     initParams = {  
  4.             @WebInitParam(name = "loginUI", value = "/home/loginUI"),  
  5.             @WebInitParam(name = "loginProcess", value = "home/login"),  
  6.             @WebInitParam(name = "encoding", value = "utf-8")  
  7.     })  
  8. public class LoginFilter implements Filter {  
  9.     private FilterConfig config;  
  10.       
  11.     @Override  
  12.     public void init(FilterConfig config) throws ServletException {  
  13.         this.config = config;  
  14.     }  
  15.   
  16.   
  17.     @Override  
  18.     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)  
  19.             throws IOException, ServletException {  
  20.         // 获取配置参数  
  21.         String loginUI = config.getInitParameter("loginUI");  
  22.         String loginProcess = config.getInitParameter("loginProcess");  
  23.         String encoding = config.getInitParameter("encoding");  
  24.   
  25.   
  26.         HttpServletRequest request = (HttpServletRequest) req;  
  27.         HttpServletResponse response = (HttpServletResponse) res;  
  28.           
  29.         // 设置请求的字符集(post请求方式有效)  
  30.         request.setCharacterEncoding(encoding);  
  31.           
  32.         // 不带http://域名:端口的地址  
  33.         String uri = request.getRequestURI();  
  34.         if (uri.contains(loginUI) || uri.contains(loginProcess)) {  
  35.             // 请求的登录,放行  
  36.             chain.doFilter(request, response);  
  37.         } else {  
  38.             if (request.getSession().getAttribute("user") == null) {  
  39.                 // 重定向到登录页面  
  40.                 response.sendRedirect(request.getContextPath() + loginUI);  
  41.             } else {  
  42.                 // 已经登录,放行  
  43.                 chain.doFilter(request, response);  
  44.             }  
  45.         }  
  46.     }  
  47.       
  48.     @Override  
  49.     public void destroy() {  
  50.         this.config = null;  
  51.     }  
  52. }  

2.方式二,web.xml方式配置

通过在web.xml文件中配置,去掉方式一中的@WebFilter注解,其余代码相同

[html] view plain copy
  1. <filter>  
  2.     <filter-name>loginFilter</filter-name>  
  3.     <filter-class>cn.edu.njit.filter.LoginFilter</filter-class>  
  4.     <init-param>  
  5.         <param-name>loginUI</param-name>  
  6.         <param-value>/home/loginUI</param-value>  
  7.     </init-param>  
  8.     <init-param>  
  9.         <param-name>loginProcess</param-name>  
  10.         <param-value>home/login</param-value>  
  11.     </init-param>  
  12.     <init-param>  
  13.         <param-name>encoding</param-name>  
  14.         <param-value>utf-8</param-value>  
  15.     </init-param>  
  16. </filter>  
  17. <filter-mapping>  
  18.     <filter-name>loginFilter</filter-name>  
  19.     <url-pattern>/*</url-pattern>  
  20. </filter-mapping>  

3.注

(1).Filter和Servlet比较相似,从属性以及配置方式上可以看出,可以理解为Servlet的加强版;

(2).Filter中对权限的过滤、字符编码的处理、日志的记录可以看成是各个Servlet中重复代码的抽取;

(3).对于字符编码的处理,request.setCharacterEncoding()对post方式的请求有效;若是get方式,可以使用new String(xxx.getBytes("iso-8859-1"), "utf-8")进行处理,否则表单的中文会乱码;也可以使用代理方式,每当通过request.getParameter()时自动进行编码处理;

猜你喜欢

转载自blog.csdn.net/lzh_86/article/details/80808384