The previous blog learnedServlet, let's get to know it this timeFilter, simply speakingFilteris aServletspecification of a technology that is not aServlet. It is also called a filter that is used to change aRequestand modify aResponse, can be in aRequestArriveservletpretreatment beforeRequest, you can alsoResponseLeaveservletTime ProcessingResponse.
First, life cycle
Previous article also written on servlet life cycle, filter and servlet similar:
1 , load when starting server filter init () method to initialize the instance; filter The instance precedes servlet )
2 , only methods are called on each request DoFilter () for processing
3 , stopping the server when calling Destroy () method, destroying the instance; (destroying an instance later than Servlet )
(filter implements The filter interface for the Javax.servlet package , including the method: init (), Dofilte (), Destroy () )
Second, How to use the Filter
To add a user as an example, compare the differences before and after use:
1 , do not use Filter :
2 , using Filter :
Compare using filter convenience: we know that a system can not only add user features, we also have to delete users, modify users, and many other features that need to be implemented, however, setting the character set is required for each function, if there is no filter dofilter request after memory interception, each jsp pages can be set to the character set. Let's look at a filter
Packagecom.tgb.drp.util.filter; Importjava.io.IOException; Importjavax.servlet.filter;importjavax.servlet.filterchain;importjavax.servlet.filterconfig; importjavax.servlet.servletexception;importjavax.servlet.servletrequest;importjavax.servlet.servletresponse;/* * * Unified processing Character set using Filter * @author Youngjong * */public Classcharsetencodingfilter implements Filter {private String encod ing;/** * Destroy Method */publicvoid Destroy () {//todo auto-generated method stub}/** * Implement DoFilter */publicvoid DoFilter (servletr Equest request, Servletresponse Response,filterchainchain) throws IOException, Servletexception {//Set character set request. Setcharacterencoding ("GB18030"); Continuation of the implementation of Chain.dofilter (Request,response); }/** * Initialize Method */publicvoid init (Filterconfig filterconfig) throws Servletexception {//todo auto-generated methods stub This.encoding=filterconfig.getinitparameter ("encoding"); System.out.println ("Charsetencodingfilter.init ()-->>endcoding=" + encoding); } }
of course, we still need to Web. XML in the configuration:
<filter><filter-name>CharsetEncodingFilter</filter-name><filter-class> Com.tgb.drp.util.filter.charsetencodingfilter</filter-class><init-param> <param-name> Encoding</param-name> <param-value>GBK</param-value></init-param></filter>
iii. comparison with the servlet :
&NBSP;&NBSP;&NBSP, and servlet The same thing, they all need to implement the interface, but filter implementation is filter servlet httpservlet web.xml
1.Servlet is to handle the client's request primarily and send its results to the client.
2.Filter Yes: Intercept the customer's httpservletrequest before HttpServletRequest arrives at the servlet. Intercept HttpServletResponse before HttpServletResponse arrives at the client. The main is to intercept, do the corresponding treatment and then transfer.
That's pretty much the stuff, but it's not going to be so good?! So make a summary: Watch the video that says: filter embodies a pattern responsibility chain mode; servlet Template method mode. About filter serlet