Filter As the name is filter, in JSP / servlet, Filter's function is to serve as the filter feature of Request and Response. When the user's request is sent to a resource, you can make some pre-processing for Request, for example Verify, filtering unsafe requests, monitoring the source and origin of the statistical request, Filter's functionality is two-way, you can also filter or perform some processing on the JSP / Servlet's Response, such as the output first compression, The presentation of XML is converted, and so on.
All in all, FILTER can serve as a mediation handler between browser and JSP / servlet, some Request's pre-processing action and some resequent processing of Response, can be delivered to this intermediary handler, of course, Filter can be achieved. Function, we can also write directly in the JSP / Servlet, but if there are many JSP / servlets that require the same processing, such as some web pages require a unified authentication mode, computing verification in every web page. The code, we better write Filter directly to make it in uniform.
Filter is actually a pure Java category, it is necessary to work on the Javax.Servlet.Filter interface, and there are three ways that must be in this interface: init (), Destory () and Dofilter (). INIT () is the method that the Filter category is loaded, and Destory () is a method that will execute at the end of the Filter's lifecycle. As for DOFILTER () is the core of the Filter function, you want Filter to be completed Work, write it, let's write a simple Filter class, as follows:
Package demo.filters;
Import java.io. *;
Import javax.servlet. *;
Import javax.servlet.http. *;
Public class filterdemo1 implements filter {
Public void init (filterconfig config) throws servletexception {
}
Public void destroy () {
}
Public void Dofilter (ServletRequest Request, ServletResponse Response, Filterchain Chain)
THROWS IOException, servletexception {
PrintWriter out = response.getwriter ();
Out.println ("Filter 1 is doing !!!");
Chain.dofilter (Request, Response);
Out.println ("Filter 1's Work IS DONE");
}
}
In this program, we just show some text, indicating that the Filter's DOFILTER () method is executed, and DOFILTER () is incorporated into three parameters, servletRequest, servletResponse, filterchain, we can get the request with the response object to them Treatment, like setting headers, encoding formats, or packaging compression functions, etc., as FILTERCHAIN is about Filter execution order, this order is set in Web.xml, you must call its Dofilter () method, You can execute the next filter if it is the last Filter, execute the JSP / Servlet web page requested by the client. In order to be able to use Filter, you must write some settings in web.xml, one setting example is as follows:
filter>
filter-mapping>
If the
Package demo.filters;
Import java.io. *;
Import javax.servlet. *;
Import javax.servlet.http. *;
Public class filterdemo2 implements filter {
Public void init (filterconfig config) throws servletexception {
}
Public void destroy () {
}
Public void Dofilter (ServletRequest Request, ServletResponse Response, Filterchain Chain)
THROWS IOException, servletexception {
PrintWriter out = response.getwriter ();
Out.println ("Filter 2 is doing !!!);
Chain.dofilter (Request, Response);
Out.println ("Filter 2's Work IS DONE");
}
}
The content set in Web.xml is:
Web.xml
filter>
filter-mapping>
filter>
filter-mapping>
Then ask any of the resources under the web application, will execute FilterDemo1 first, then perform FilterDemo2, the resource of the final call request, if the resource we request is a JSP page, and its function is just printing 10 Hello! World!
<%
For (int i = 0; i <10; i )
OUT.PRINTLN ("Hello! World!");
%>
The result is the result of the JSP page, the result is:
Filter 1 is doing !!! Filter 2 is doing !!! Hello! World!
Hello! World! Hello! World! Hello! World! Hello! World!
Hello! World! Hello! World! Hello! World! Hello! World!
Hello! World! Filter 2's Work IS Done Filter 1's Work Is Done