The Servlet API provides a filter interface that, when developing a Web application, if the Java class is written to implement this interface, the Java class is called filter filter. Through the filter
technology, developers can enable users to intercept access requests and responses before accessing a target resource. It allows the user to change a request and modify a response. Filter
not a servlet, it cannot produce a response, it can preprocess the request before a request arrives at the servlet, or it can handle response when response leaves the servlet.
in other words, filter is actually a "servlet chaining" (servlet chain).
Including
1. Intercept before the servlet is invoked;
2. Check the servlet request before the servlet is invoked;
3. Modify the request header and request data as required;
4. Modify the response head and response data as required;
5. Intercepted after the servlet is invoked.
implementation steps:
1. Create a class implements Filter.
2, implement the Dofilter () method.
3. Configuring in Web. XML (refer to servlet configuration)
eg.
<filter-mapping>
<filter-name>Demo1Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher> <!--Not Configured dispatcher is the default REQUEST method--
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
4. Visit a page to see if you can intercept.
Filter Application Scenario:
By understanding filter filter, you can know that in the following three situations can do some processing:
1> determines whether the target resource needs to be accessed by controlling the invocation of the Chain.dofilter method.
For example, user permissions can be verified and so on. Determine whether the user has access to certain resources, have permission to release, do not have permission to execute the Chain.dofilter method.
2> to achieve some purpose by doing some processing before calling the Chain.dofilter method.
For example, solve the problem of Chinese garbled and so on. The encoding of the request encoding and response can be performed before the Dofilter method. The request interface can even be encapsulated to handle
Get request way of Chinese garbled problem (rewrite the corresponding Request.getparameter method).
3> does this by doing some processing after invoking the Chain.dofilter method to achieve some purpose.
For example, the entire Web site compression. Encapsulates the response object with Class A before calling the Chain.dofilter method, overriding Getoutputstream and overriding Getwriter
method. Inside Class A, the output is cached in the Bytearrayoutputstream stream, and then after the Chain.dofilter method is executed, get Class A Bytearrayoutputstream
stream cache data, compressed with a gzipoutputstream stream.
eg.
Filter life cycle:
As with Servlets, the creation and destruction of the filter is also the responsibility of the Web server. However, unlike the servlet, it is
1> Loads the filter class when the app is started (same as the servlet's load-on-startup configuration).
after the 2> container has created the Filter object instance, call the Init () method. The Web container is then saved into the application-level collection container and waits for the user to access the resource.
3> When a user accesses a resource that is intercepted by the url-pattern of the filter, the container takes out the filter class and calls the Dofilter method, and the next or multiple access to the intercepted resource, the Web
the container directly takes out the specified Filter object instance and calls the Dofilter method (the filter object resides in the Web container).
4> when the application service is stopped or reloaded, the filter's Destroy method is executed and the filter object is destroyed.
Note: The Init method and the Destroy method will only be used directly once.
Listener Listener
Listener Listener is a functional component that automatically executes code when a application,session,request three objects are created, destroyed, or added to a modified delete property. Listener
is a servlet listener that listens to client requests, service-side operations, and so on.
Classification and use of listener
There are three main categories:
1, ServletContext monitoring
Servletcontextlistener: Used to listen (create, destroy) the entire context of the servlet.
public void contextinitialized (Servletcontextevent SCE);//context initialization public
void contextdestroyed ( Servletcontextevent SCE);//context Destroy public
ServletContext getservletcontext ();// Servletcontextevent event: Get a ServletContext (application) object
Servletcontextattributelistener: Listener for the servlet context property (the Redaction property).
public void attributeadded (servletcontextattributeevent scab);//Add Property public
void Attributeremoved ( Servletcontextattributeevent scab);//property Delete public
void attributerepalced (servletcontextattributeevent scab);// Property substitution (Set the same property for the second time)
//servletcontextattributeevent event: Can get the name of the setting property and the content public
String getName ();//Get the property name
Public Object getValue ();//Gets the value of the property
2. Session Monitoring
The session belongs to the content under the HTTP protocol, and the interface is under the javax.servlet.http.* package.
Httpsessionlistener Interface: Monitor the overall state of the session.
public void sessioncreated (httpsessionevent se);//session create public
void sessiondestroyed (httpsessionevent se);// Session Destruction
//httpsessionevent event: Public
HttpSession getsession ();//session with current operation
Httpsessionattributelistener interface: Listen to the session's properties.
public void attributeadded (httpsessionbindingevent se);//Add Property public
void Attributeremoved ( Httpsessionbindingevent se);//Delete the property public
void attributereplaced (httpsessionbindingevent se);/
/replace attribute// Httpsessionbindingevent event: Public
String getName ();//Gets the name of the property public
Object GetValue ();//Gets the value of the property public
HttpSession getsession ();//Get the current session
3. Request Monitoring
Servletrequestlistener: Used to listen for request requests (create, destroy).
public void requestinitialized (Servletrequestevent SRE);//request initialize public
void requestdestroyed ( Servletrequestevent SRE);//request destroy
//servletrequestevent event: Public
servletrequest getservletrequest (); /Get a ServletRequest object public
ServletContext getservletcontext ();//Get a ServletContext (application) object
Servletrequestattributelistener: Listens for the request property (the Redaction property).
public void attributeadded (Servletrequestattributeevent srae);//