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