Filter intercepts all web resources managed by the Web server, such as JSP, Servlet, static image files, or static html files, to implement some special functions. For example, some advanced functions such as URL-level access control, filtering sensitive words, and compressing response information are implemented.
1 life cycle: the server starts to execute the init method. When the interception is called, the server executes the dofilter method and closes the destroy method.
2 instances
1)Batch Set Request Encoding
Public class encodingfilter implements filter {
Private string encoding = NULL;
Public void destroy (){
Encoding = NULL;
}
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
String encoding = getencoding ();
If (encoding = NULL ){
Encoding = "gb2312 ";
}
Request. setcharacterencoding (encoding); // set the specified encoding in the request
Chain. dofilter (request, response );
}
Public void Init (filterconfig) throws servletexception {
This. Encoding = filterconfig. getinitparameter ("encoding ");
}
Private string getencoding (){
Return this. Encoding;
}
}
XML Code
<Filter>
<Filter-Name> encodingfilter </filter-Name>
<Filter-class> com. logcd. Filter. encodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> gb2312 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> encodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
2)Use filter to control user access
Public void dofilter (servletrequest request,
Servletresponse response,
Filterchain chain)
Throws ioexception, servletexception {
Httpservletrequest Req = (httpservletrequest) request;
Httpservletresponse res = (httpservletresponse) response;
Httpsession session = Req. getsession ();
If (session. getattribute ("username ")! = NULL) {// access only after Logon
Chain. dofilter (request, response );
} Else {
Res. sendredirect ("../failure. jsp ");
}
}
XML Code
<Filter>
<Filter-Name> securityfilter </filter-Name>
<Filter-class> com. logcd. Filter. securityfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> securityfilter </filter-Name>
<URL-pattern>/admin/* </url-pattern>
</Filter-mapping>
3. Filter execution sequence
When there are multiple filters, the execution order is the method of filtering chain. The dofilter () method of multiple filters is executed at a time. follow the web. the filters defined in XML are sequentially assembled into a chain. First run the method before chain. dofilter () and then run the method after chain. dofilter () from the back to the front.
The preceding execution sequence is as follows:
The part before chain. dofilter () in encodingfilter. dofilter () is executed: request. setcharacterencoding ("gb2312 ");
Run the section before chain. dofilter () in securityfilter. dofilter () to determine whether the user has logged on.
If the user has logged on, the requested resource is/admin/index. jsp.
If the user is not logged on, the page will be redirected to:/failure. jsp.
The part after chain. dofilter () in securityfilter. dofilter () is executed: no code is available here.
The part after chain. dofilter () in encodingfilter. dofilter () is executed: no code is available here.
References: http://log-cd.iteye.com/blog/423179
Filter knowledge Summary