Implement the filter interface as Genericservlet implements the Servlet interface

Source: Internet
Author: User

When writing filter, we need to implement the filter interface, need to implement Init (Filterconfig arg0), DoFilter (ServletRequest request, servletresponse response, Filterchain chain), destroy () and so on, sometimes we don't need to implement the Init and Destory methods, but we have to write these two methods in the code. If we don't want to write this two more ways, we can use a class to implement the filter interface, In our development, we inherit this class directly and implement the Dofilter method.

The code is as follows:

1  PackageCom.zhx.http.filter;2 3 Importjava.io.IOException;4 5 ImportJavax.servlet.Filter;6 ImportJavax.servlet.FilterChain;7 ImportJavax.servlet.FilterConfig;8 Importjavax.servlet.ServletException;9 Importjavax.servlet.ServletRequest;Ten ImportJavax.servlet.ServletResponse; One Importjavax.servlet.http.HttpServletRequest; A ImportJavax.servlet.http.HttpServletResponse; -  - /** the  *  -  * @authorzhaoxin - * Custom HTTPFilter, implement filter interface, refer to self-genericservlet implementation mode -  */ +  Public Abstract classHTTPFilterImplementsfilter,java.io.serializable { -      +     Private Static Final LongSerialversionuid = 1L; A      at     Privatefilterconfig Filterconfig; -  -     /** - * This method is called by the servlet container and is called only once - * Subclasses are not recommended to override this method directly -      */ in @Override -      Public voidInit (Filterconfig arg0)throwsservletexception { to          This. Filterconfig =arg0; +          This. Init (); -     } the      *     /** $ * For subclasses to override the initialization method, in the subclass can call the Getfilterconfig () method in the Init () method to get the Filterconfig object, to get the initialization parametersPanax Notoginseng      * @throwsservletexception -      */ the     protected voidInit ()throwsservletexception { +         //TODO auto-generated Method Stub A     } the      +     /** - * For sub-class invocation, to get the Filterconfig object $      * @return $      */ -      Publicfilterconfig Getfilterconfig () { -         returnFilterconfig; the     } -     Wuyi     /** the * This method is called by the servlet container and is not recommended to be overridden directly -      */ Wu @Override -      Public voidDoFilter (servletrequest req, servletresponse Res, AboutFilterchain FC)throwsIOException, servletexception { $          - httpservletrequest request; - httpservletresponse response; -          A         Try { +Request =(httpservletrequest) req; theResponse =(HttpServletResponse) res; -}Catch(Exception e) { $              Throw NewServletexception ("Non-http Request or response"); the         } the DoFilter (Request, response, FC); the     } the      -     /** in * This method must be overridden by a subclass of filtering methods the      * @paramRequest the      * @paramResponse About      * @paramFilterchain the      * @throwsIOException the      * @throwsservletexception the      */ +      Public Abstract voidDoFilter (httpservletrequest request, httpservletresponse response, -Filterchain Filterchain)throwsIOException, servletexception; the Bayi     /** the * Called by the servlet container the      */ - @Override -      Public voiddestroy () { the         //TODO auto-generated Method Stub the  the     } the  -}

When developing, we just need to create a new class and then inherit the HTTPFilter. If there is a need for initialization, it is recommended to override the parameterless init () method, if you want to use the Filterconfig object within the method, by calling This.getfilterconfig () method is available. Rewrite Dofilter (httpservletrequest request, HttpServletResponse Response,filterchain Filterchain) is recommended when writing a filtering method. The advantage of this method is that httpservletrequest,httpservletresponse can be used directly, without having to go back to ServletRequest, The Servletresponse object is cast to the Httpservletrequest,httpservletresponse object. If you have operations such as releasing resources, rewrite the Destory () method directly, If you don't, you don't have to rewrite it. You can make your code look more beautiful.

Implement the filter interface as Genericservlet implements the Servlet interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.