星空网 > 软件开发 > 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()方法即可,没有的话就不用重写.可以让代码看上去更美观.

 




原标题:像GenericServlet实现Servlet接口的方式去实现Filter接口

关键词:接口

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok国际版下载旧版:https://www.goluckyvip.com/tag/83692.html
tiktok国际版旧版下载:https://www.goluckyvip.com/tag/83693.html
国际版tiktok下载安卓教程:https://www.goluckyvip.com/tag/83694.html
tiktok安卓免费下载:https://www.goluckyvip.com/tag/83695.html
tiktok俄罗斯版下载:https://www.goluckyvip.com/tag/83696.html
tiktok国际下载安卓:https://www.goluckyvip.com/tag/83697.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流