The role of the filter
1. When a user requests a web resource, if there is no filter, the user can directly get to the Web resource, when the user requests a Web resource, the filter in the Web container will intercept the request and then handle it accordingly, then access the resource corresponding to the request. , this resource is returned to the filter and returned to the user by the filter, which is probably the way the filter works.
Next, let's talk about the filter's life cycle;
The first thing to instantiate this filter, the time to instantiate is when the Web. XML is loaded, then initialize the filter, initialize calls the Init () method, both of which are executed only once, that is, when the website container is started, the next step is to filter, filter calls Dofilter ( ) method, which is then the destroy phase, the Destory () method, which is called only once, when the Web container is closed,
Init () This is the initialization method of the filter, which is called after the Web container creates the filter instance. The parameters of the filter in the Web. xml file can be read in this method;
DoFilter () This method completes the actual filtering operation, this place is the core method of the filter, when the user requests to access the URL associated with the filter, the Web container will first call the filter's DoFilter method;
The Fileterchain parameter can call the Chain.dofiter method, pass the request to the next filter (or target resource), or, using forwarding, redirect the request to another resource.
The Destory:web container calls this method before destroying the filter instance, in which the resource used by the filter can be freed
To use the filter to configure this filter in Web. xml
Configure the filter in Web. XML, in addition to writing code, you can also use the myeclipse in the design of the shortcut, select Filter;
Firstfilter.java
PackagecomImportjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classFirstfilterImplementsFilter {@Override Public voiddestroy () {System.out.println ("Destroy---firstfilter"); } @Override Public voidDoFilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {System.out.println ("Start----dofilter--firstfilter");//Chain.dofilter (request, response);HttpServletRequest req =(httpservletrequest) request; HttpServletResponse Response2=(httpservletresponse) response; // redirectResponse2.sendredirect (Req.getcontextpath () + "/main.jsp"); //forwarding//req.getrequestdispatcher ("main.jsp"). Forward (request, response);//req.getrequestdispatcher ("main.jsp"). Include (request, response);SYSTEM.OUT.PRINTLN ("End------Dofilter--firstfilter"); } @Override Public voidInit (Filterconfig filterconfig)throwsservletexception {System.out.println ("Init----firstfilter"); }}
View Code
Problem Description:
1. Can the filter change the Web resources requested by the user? In other words, can you change the path of the user request? Can
2. Can the filter directly return data, can you directly handle user requests? (no), the filter cannot be returned directly meaning that the user request cannot be processed directly, nor can it generate a response directly to the client. The response object is HttpServletResponse, which is already the response data returned by the Web resource being accessed. The filter is not a standard servlet, it is mainly used to preprocess the HttpServletRequest, or the httpservletresponse can be processed later.
Question: How are multiple filters implemented in a Web project? What is the order of multiple filters corresponding to the same user path execution?
In a Web project where multiple filters are allowed to be defined, if the filters are irrelevant, a filter chain is formed, so how does the code in Dofiter work?
The server is assembled into a chain in the order defined by the filter in Web. Xml.
Secondfilter.java
PackagecomImportjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classSecondfilterImplementsFilter {@Override Public voiddestroy () {System.out.println ("Destroy-----Secondfilter"); } @Override Public voidDoFilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {System.out.println ("Start---dofilter--secondfilter"); Chain.dofilter (request, response); System.out.println ("End---dofilter--secondfilter"); } @Override Public voidInit (Filterconfig filterconfig)throwsservletexception {System.out.println ("Init-----secondfilter"); }}
View Code
It executes the code that releases the money for each filter in the filter chain in turn, then executes the code of the Web resource code, and then sequentially executes the pass-through of each filter in the filter chain.
Java Web Development Application----Filter