1. First, what is the filter?
The filter technology allows users to change a request and modify a response. Filter is not a servlet. It cannot generate a response. It can pre-process a request before it reaches the servlet, or process response when it leaves the servlet. In other words, the filter is actually a "servlet chaining" (servlet chain ).
A filter includes:
1. intercepted before the servlet is called;
2. Check the Servlet request before the servlet is called, such as unified encoding conversion.
3. Modify the request header and request data as needed; decoration or dynamic proxy implementation
4. Modify the response header and response data as needed;
5. After the servlet is called, it is intercepted.
It can capture servlet running results, such as data compression.
All mysteries are in the filterchain of filter. The server assembles a chain according to the sequence defined by the filter in Web. XML, and then executes the dofilter () method at one time.
2. lifecycle of the filter
The lifecycle of the filter: (the three methods Init (), dofilter (), and destroy () of the filter interface of the javax. servlet package must be implemented. Empty implementations are also supported)
(1) load the filter instance when starting the server, and call the init () method to initialize the instance;
(2) Only the method dofilter () is called for processing each request;
(3) Call the destroy () method when stopping the server to destroy the instance.
3. The problem of garbled GET requests using Filters
First, write a simple JSP page.
Second, servlet is also relatively simple and is not written.
The key is Web configuration and filter.
Add in Web configuration
<filter><filter-name>Utf8Filter</filter-name><filter-class>cn.csdn.web.utf8.Utf8Filter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>Utf8Filter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
Filter
Package CN. csdn. web. utf8; import Java. io. ioexception; import Java. io. unsupportedencodingexception; import javax. servlet. filter; import javax. servlet. filterchain; import javax. servlet. filterconfig; import javax. servlet. servletexception; import javax. servlet. servletrequest; import javax. servlet. servletresponse; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletrequestwrapper; public class utf8filter implements filter {private string encoding = NULL; Public void Init (filterconfig) throws servletexception {encoding = filterconfig. getinitparameter ("encoding");} class heyley extends httpservletrequestwrapper {string UTF = NULL; // make it a utf-8public string getparameter (string name) {try {system. out. println (encoding); UTF = new string (super. getparameter (name ). getbytes ("iso8859-1"), encoding);} catch (unsupportedencodingexception e) {// todo auto-generated catch blocke. printstacktrace ();} return UTF;} public heyley (httpservletrequest request) {super (request) ;}} public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {request = new heyley (httpservletrequest) request); chain. dofilter (request, response);} public void destroy (){}}
Original: http://download.csdn.net/detail/rhljiayou/3834731