你的位置:首页 > Java教程

[Java教程]像GenericServlet实现Servlet接口的方式去实现Filter接口


编写Filter的时候,我们需要实现Filter接口,需要去实现init(FilterConfig arg0),doFilter(ServletRequest request, ServletResponse response, FilterChain chain),destroy()等方法,有时候我们并不需要去实现init与destory方法,但又必须在代码里面写上这两个方法.如果我们不想多写这个两个方法,我们可以用一个类去实现Filter接口,在我们开发的时候直接继承这个类,实现doFilter方法即可.

代码如下:

 1 package com.zhx.http.filter; 2  3 import java.io.IOException; 4  5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest;10 import javax.servlet.ServletResponse;11 import javax.servlet.http.HttpServletRequest;12 import javax.servlet.http.HttpServletResponse;13 14 /**15  * 16  * @author zhaoxin17  * 自定义的HttpFilter,实现Filter接口,参考自GenericServlet的实现方式18 */19 public abstract class HttpFilter implements Filter,java.io.Serializable {20   21   private static final long serialVersionUID = 1L;22   23   private FilterConfig filterConfig;24 25   /**26    * 该方法由servlet容器进行调用,有且只调用一次27    * 不建议子类直接重写该方法28   */29   @Override30   public void init(FilterConfig arg0) throws ServletException {31     this.filterConfig = arg0;32     this.init();33   }34   35   /**36    * 供子类重写的初始化方法,在子类中可以在init()方法中调用getFilterConfig()方法来获取FilterConfig对象,来获取初始化参数37    * @throws ServletException38   */39   protected void init() throws ServletException {40     //TODO Auto-generated method stub41   }42   43   /**44    * 供子类调用,来获取FilterConfig对象45    * @return46   */47   public FilterConfig getFilterConfig() {48     return filterConfig;49   }50   51   /**52    * 该方法由servlet容器调用,不建议之类直接进行重写53   */54   @Override55   public void doFilter(ServletRequest req, ServletResponse res,56       FilterChain fc) throws IOException, ServletException {57     58     HttpServletRequest request;59     HttpServletResponse response;60     61     try {62       request = (HttpServletRequest)req;63       response = (HttpServletResponse)res;64     } catch (Exception e) {65       throw new ServletException("non-HTTP request or response");66     }67     doFilter(request, response, fc);68   }69   70   /**71    * 该方法由子类必须重写的过滤方法72    * @param request73    * @param response74    * @param filterChain75    * @throws IOException76    * @throws ServletException77   */78   public abstract void doFilter(HttpServletRequest request, HttpServletResponse response,79       FilterChain filterChain) throws IOException, ServletException;80 81   /**82    * 由servlet容器进行调用83   */84   @Override85   public void destroy() {86     // TODO Auto-generated method stub87 88   }89 90 }

开发的时候我们只需新建一个类,然后去继承HttpFilter即可.如果有初始化的需要,建议重写无参的init()方法,如果想在该方法内使用FilterConfig对象,通过调用this.getFilterConfig()方法即可获得.编写过滤方法时建议重写doFilter(HttpServletRequest request, HttpServletResponse response,FilterChain filterChain).该方法的好处是可以直接使用HttpServletRequest,HttpServletResponse,不用自己再去将ServletRequest,ServletResponse对象强制转换为HttpServletRequest,HttpServletResponse对象.如果有释放资源等操作的时候,直接重写destory()方法即可,没有的话就不用重写.可以让代码看上去更美观.