Implement the Filter interface through Servlet interface implementation like GenericServlet to implement servlet Interface
When writing a Filter, we need to implement the Filter interface and implement methods such as init (FilterConfig arg0), doFilter (ServletRequest request, ServletResponse response, FilterChain chain), and destroy, sometimes we do not need to implement the init and destory methods, but we must write these two methods in the code. if we don't want to write these two methods, we can use a class to implement the Filter interface. During our development, we can directly inherit this class and implement the doFilter method.
The Code is as follows:
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 * Custom HttpFilter to implement the Filter interface, refer to the implementation method of self-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/*** this method is called by the servlet container, yes and only one call 27 * It is not recommended that subclass rewrite this method 28 */29 @ Override30 public void init (FilterConfig arg0) throws ServletException {31 this. filterConfig = arg0; 32 this. init (); 33} 34 35/** 36 * indicates the initialization method to be rewritten by the subclass. In the subclass, you can call the getFilterConfig () method in the init () method to obtain the FilterConfig object, to get the initialization parameter 37 * @ throws ServletException38 */39 protected void init () throws ServletException {40 // TODO Auto-generated method stub41} 42 43/** 44 * For subclass calls to obtain the FilterConfig object 45 * @ return46 */47 public FilterConfig getFilterConfig () {48 return filterConfig; 49} 50 51/** 52 * this method is called by the servlet container. It is not recommended to rewrite 53 directly */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 = (response) res; 64} catch (Exception e) {65 throw new ServletException ("non-HTTP request or response"); 66} 67 doFilter (request, response, fc ); 68} 69 70/** 71 * the filtering method that must be overwritten by the subclass 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 * The servlet container calls 83 */84 @ Override85 public void destroy () {86 // TODO Auto-generated method stub87 88} 89 90}
During development, we only need to create a new class and inherit HttpFilter. if Initialization is required, we recommend that you override the init () method without parameters. If you want to use the FilterConfig object in this method, call this. the getFilterConfig () method can be obtained. we recommend that you rewrite doFilter (HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) when writing a filtering method ). the advantage of this method is that you can directly use HttpServletRequest and HttpServletResponse. You do not need to forcibly convert the ServletRequest and ServletResponse objects to HttpServletRequest and HttpServletResponse objects. if the destory () method is directly rewritten when resources are released or other operations are performed, the destory () method is not required. it makes the Code look more beautiful.