1. What is a filter?
In the client-to-server process, when the request is sent, if there is non-compliant information will be blocked by the filter, if the compliance will be released, when the server responds to the client will also be judged if there is non-compliant information will be blocked by the filter, if the compliance will be released.
Oop:java object-oriented programming, abstraction, encapsulation, inheritance, polymorphism.
AOP: Plane-oriented programming, filter is a face-oriented programming idea.
AOP is a new feature of the Sun company srvlet2.3 release, before the 2.3 version of the feature, the definition of a filter needs to implement the (implement) filter interface, the implementation is javax.servlet.Filter.
A filter is a program that runs on the server prior to the servlet or JSP page associated with it. Filters can be attached to one or more servlet or JSP pages, and can be checked for request information that enters these resources. After that, the filter can be chosen as follows: ① calls the resource (that is, invokes the servlet or JSP page) in a regular manner. ② invokes the resource with the modified request information. ③ invokes the resource, but modifies it before sending the response to the client. ④ blocks the resource call and instead goes to another resource, returns a specific status code, or generates a replacement output.
2, the life cycle of the filter:
When the project starts, the filter begins to initialize, and when the request comes, it starts to execute the Dofilter method automatically, as the project's close filter starts to stop.
3. What is the use of filters?
The main role of the filter is to filter the request, through the filter technology, Web server management of all Web resources: For example: JSP, Servlet, static picture files, or static HTML files for interception, to achieve some special functions, such as: the implementation of URL-level permissions control, Some advanced features such as sensitive words, compressed response information, and more.
4, how to use it?
Can write multiple, from user access-"first filter-" Second filter--"Servlet
Filter life cycle: As the project starts and is created, when the address is accessed and the Dofilter method is called, this is executed multiple times and destroyed as the project closes.
There are three parameters when calling the Dofilter method:
Reqest (ServletRequest)--"httpservletrequest (note strong turn: In the use of the unique method of HttpServletRequest to strong turn)
Response (Servletresponse)--"httpservletresponse (note strong turn: In the use of the unique method of HttpServletResponse to strong turn)
The Filterchain chain can point to the next address (if there is a filter pointing to the next filter, without pointing to the resource servlet) filtering, blocking, releasing
Chain.dofilter (REQ,RESP);//Release
chainfilter Chain:
It can point to the next resource, and if there is a filter, continue with the next filter, or, if there is no filter, point to the resource (servlet).
Filter Application Scenarios:
(1). Set the encoding for all Servlets
(2). Setting cross-domain issues
(3). Resolve all anti-theft chain issues
Files that need to be configured when using filters:
(1), fixed address
(2), wildcard characters
(3), designated directory
The filter code is:
package servlet;import javax.servlet.*;import javax.servlet.annotation.webservlet;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import java.io.ioexception;/** * urlpattern={"/*"} Gets the mapping path for all servlets and implements the interaction between the different servlets */@WebServlet ( name = "Demofilter", urlpatterns = {"/*"}) public class demofilter implements Filter{ /** * How to initialize filters * @param filterConfig You can get the filter initialization parameters that are allocated in the deployment descriptor file (Web. xml). * @throws ServletException */ @Override public void init (filterconfig filterconfig) Throws servletexception { system.out.println ("The filter was created "); } /** * * @param servletRequest Requests * @param servletResponse Response * @param filterChain chain, block and release filters * @throws IOException * @throws ServletException */ @Override public void dofilter (Servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws IOException, servletexception { system.out.println ("Filter was carried out"); servletresponse.setcontenttype ("Text/html,charset=utf-8"); HttpServletRequest request = ( HttpServletRequest) servletrequest; httpservletresponse response = (HttpServletResponse) servletresponse; response.addheader (" Access-control-allow-origin "," * "); string referer = request.getheader ("Referer"); system.out.println (Referer) ; filterchain.dofilter (servletrequest,servletresponse);//Release } @Override public void Destroy () { system.out.println ("Filter Destroyed"); }}
Java Filter (filter and Chainfilter chain) do you understand? (with detailed code)