"Java EE Enterprise Application Combat Learning record" Filter

Source: Internet
Author: User

1  PackageSanglp.servlet;2 3 Importjavax.servlet.*;4 ImportJavax.servlet.annotation.WebFilter;5 Importjavax.servlet.http.HttpServletRequest;6 Importjava.io.IOException;7 8 /**9 * Created by Administrator on 2016/10/5.Ten  */ One@WebFilter (filtername = "Log", urlpatterns={"/*"} ) A  Public classLogfilterImplementsFilter { -  -     //configuration information for accessing the filter the     Privatefilterconfig config; -  -  - @Override +      Public voidInit (Filterconfig filterconfig)throwsservletexception { -          This. config=Filterconfig; +     } A  at @Override -      Public voiddestroy () { -          This. config=NULL; -     } -  - @Override in      Public voidDoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain filterchain)throwsIOException, servletexception { -         //preprocessing for user requests to         //get ServletContext objects used to log logs +ServletContext context= This. Config.getservletcontext (); -         LongBefore=System.currenttimemillis (); theSystem.out.println ("Start filtering"); *         //convert request to HttpServletRequest $HttpServletRequest hrequest=(HttpServletRequest) servletrequest;Panax Notoginseng         //Output hint Information -SYSTEM.OUT.PRINTLN ("Filter has intercepted the requested address of the user" +Hrequest.getservletpath ()); the         //The filter is only chained, and the request remains on the destination address. + Filterchain.dofilter (ServletRequest, servletresponse); A         //responding to post-execution processing the         LongAfter=System.currenttimemillis (); +System.out.println ("Filter End"); -SYSTEM.OUT.PRINTLN ("The request is located to" +hrequest.getrequesturi () + "takes time:" + (after-before)); $     } $}

Filter: Used to process the user request preprocessing, or httpservletresponse can be processed, is a typical processing chain.
The complete process of using the filter is that the filter pre-processes the user request, then gives the request to the servlet for processing and generates a response, and finally the filter then processes the server response.
Filter use:
Intercept the client's httpservletrequest before HttpServletRequest arrives at the servlet
Check the httpservletrequest as needed, or modify the HttpServletRequest header and data
Intercept HttpServletResponse before HttpServletResponse arrives at the client
Check the HttpServletResponse as needed, or modify the HttpServletResponse header and data
There are several types of filter:
User-Authorized Filter:filter is responsible for checking the user request, filtering the user's illegal request according to the request
Log filter: Verbose logging of certain special user requests
Filter that is responsible for decoding: Includes request decoding for non-standard encoding
XSLT filter that can change XML content, etc.
Filter can intercept multiple requests or responses, and one request or response can be intercepted by multiple filter
Create a filter:
Creating a filter processing class,-> implementation Javax.servlet.Filter excuse
Config filter in the Web. xml file

"Java EE Enterprise Application Combat Learning record" Filter

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.