Filter filters
Filter and Lister are the two advanced features in the servlet specification. Unlike Servlets, they are not used to process client requests, but only for request,
response to modify or listen to the context, session, request event .
Filter means a filter or filter that modifies the request or response outside the servlet. Filter presents the concept of a filter chain, a
Filterchain includes multiple F
1 What is a filter
Filter Javaweb One of the three components , which is very similar to a servlet! No it filters are used to intercept requests, not to process requests.
When a user requests a servlet, the filter that is deployed on the request is executed first, and if Filter is "released", the servlet that executes the user's request is inherited, and if filter is not "released", the servlet requested by the user is not executed.
It can be understood that when a user requests a servlet, Tomcat executes the filter that is registered on the request, and then whether or not "release" is determined by the filter. Can be understood as filter to determine whether to invoke servlet! After executing the code for the servlet, the code after the filter is executed.
Hello World of filter 2
In fact, the filter is very similar to the servlet, so let's recall the first servlet app if written! Write a class to implement the Servlet interface! Yes, the Write filter is to write a class that implements the filter interface.
Public class Hellofilter implements Filter { Public void init[Cui 1] (filterconfig filterconfig) throws servletexception {} Public void doFilter[Cui 2] (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception { System. out. println ("Hello Filter"); } Public void Destroy[Cui 3] () {} } |
[Cui 1] ignore it .
[Cui 2] when accessing intercepted resources, the DoFilter () method is called! Let's not take care of what its parameters are!
[Cui 3] ignore it .
[Cui 4] specify the path to intercept! Hellofilter is executed when the user accesses the index.jsp page
The second step, like the servlet, is to deploy filter in the Web. xml file:
<filter> <filter-class> Cn.itcast.filter.hellofilter</filter-class> <filter-mapping> <filter-name>hellofilter</ Filter-name> <url-pattern>/index.jsp[Cui 4] </url-pattern> </ Filter-mapping> |
There should be no problem, you can read it!
OK, now you can try to visit the index.jsp page, see what effect!
When the user accesses the Index.jsp page, the Hellofilter Dofilter () method is executed! In our example, the index.jsp page is not executed, and if you want to execute the index.jsp page, we need to release it!
Public void DoFilter (servletrequest request, servletresponse response, Filterchain chain) throws IOException, servletexception { System. out . println ("filter Start ..."); [Cui 5] [Cui 6] out . println ("Filter End ..."); [Cui 7] |
[Cui 5] Execute index.jsp before executing
[Cui 6] release! Represents the execution index.jsp
[Cui 7] Execute this sentence after the execution of index.jsp
A lot of students are always wrong to think that a request after the output to the client is finished, it is wrong! In fact, a lot of things need to respond to the client to complete!
Learning Notes _ Filter Overview (filter Javaweb One of the three components)