(1) Filter
requesting information encoding conversion
Importjava.io.IOException;
Importjava.util.Enumeration;
Importjavax.servlet.Filter;
Importjavax.servlet.FilterChain;
Importjavax.servlet.FilterConfig;
Importjavax.servlet.ServletException;
Importjavax.servlet.ServletRequest;
Importjavax.servlet.ServletResponse;
public class Encodefilter implementsfilter{
Private filterconfig filterconfig=null;
Private String Encoding=null;
//default constructor
Public Encodefilter () {
}
//init filter
public void init (Filterconfig filterconfig) Throwsserv letexception{
This.filterconfig=filterconfig;
This.encoding=filterconfig.getinitparameter ("encoding");
}
//Get encoded
Private String getencoding () {
return this.encoding;
//Perform filtering operations
public void Dofilter (Servletrequestrequest,servletresponseresponse,
Filterchainfilterc Hain) throws
ioexception,servletexception{
//Set encoding format
if (request.getcharacterencoding () ==null) {
Encoding from configuration file
String encoding=getencoding ();
Set encoding
if (encoding!=null) {
request.setcharacterencoding (encoding);
}
Pass Filter
Filterchain.dofilter (Request,response);
}
}
public void Destroy () {
Filterconfig=null;
Encoding=null;
}
}
Web.xml file Configuration
<filter>
<filter-name>SetEncodingFilter</filter-name>
<filter-class>cn.mblogger.mydeman.EncodeFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(2) Check whether the user to log on the filter
/**
Filter used to detect whether a user is logged in or not, redirect to the login page
Checksessionkey key words to be checked in session
*/
Public class Checkloginfilter implements Filter {
protected filterconfig filterconfig = null;
priv ate String redirecturl = null;
private String sessionkey = null;
public void init (Filterconfig filterconfig) throws servletexception {
This.filterconfig = Filterconfig;
redirecturl = Filterconfig.getinitparameter ("RedirectURL");
sessionkey = Filterconfig.getinitparameter ("Checksessionkey");
}
public void Dofilter (ServletRequest servletrequest,
servletresponse Servletresponse, Filterchain filterchain)
throws IOException, servletexception {
httpservletrequest request = (httpservletrequest) servletrequest;
httpservletresponse response = (httpservletresponse) servletresponse;
HttpSession session = Request.getsession ();
if (Session.getattribute (SessionKey)!=null) {//If already logged in, perform additional filter
Filterchain.dofilter (request, response); Continue to the next filter
Request.getrequestdispatcher ("/admin/index.jsp");
Return
} else {
Response.sendredirect ("/login.jsp");
}
}
public void Destroy () {
}
}
description of the filter chain
For example, the top two filter one is to process the text encoding, one is to process permissions
1, the implementation of the Encodingfilter class Dofilter () in the Chain.dofilter () before the part: request.setcharacterencoding (encoding);
2, the implementation of the Checkloginfilter class Dofilter () before the Chain.dofilter () part: To determine whether the user is logged in.
3. If the user is logged in, the requested resource is accessed:/admin/index.jsp.
4, if the user is not logged in, the page is redirected to:/login.jsp.
5, the implementation of the Checkloginfilter class Dofilter () after the Chain.dofilter () part: Execute Return statement method
6, the implementation of the Encodingfilter class Dofilter () after the Chain.dofilter () section: There is no code here.
The advantage of the filter chain is that it can be interrupted at any time during execution, as long as the Chain.dofilter () is not executed and the subsequent filter and the requested content are not executed.
And in the actual use, we should pay special attention to the chain of the implementation of the order problem, like encodingfilter must be placed in all filters
Before you can ensure that the correct encoding is set before using the data in the request.
(3) filters that enable browsers not to cache pages
Import javax.servlet.*;
Import Javax.servlet.http.HttpServletResponse;
Import java.io.IOException;
/**
* Filters used to make Browser not cache the page
*/
public class Forcenocachefilter implements Filter {
public void Dofilter (ServletRequest request, servletresponse response, Filterchain Filterchain) throws IOException, servletexception{
((httpservletresponse) response). SetHeader ("Cache-control", "No-cache");
((httpservletresponse) response). SetHeader ("Pragma", "No-cache");
((httpservletresponse) response). Setdateheader ("Expires",-1);
Filterchain.dofilter (request, response);
}
public void Destroy () {
}
public void init (Filterconfig filterconfig) throws servletexception{
}
}