Package com.test.web.filter;
Import java.io.IOException;
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.HttpServletResponse;
Import javax.servlet.http.HttpSession;
/**
* Process character transcoding, providing for the reading of HTTP-related information in spring AOP or any other instance class loaded through a Web request
* @author Pei Hexian
*
*/
public class Commonfilter implements Filter {
Private Filterconfig config = null;
Private String Defaultencode = "UTF-8";
private static final String charset_encode_param_name= "CHARSET";
private static threadlocalprivate static threadlocal
public static HttpServletRequest Getrequest () {
Return (HttpServletRequest) requests.get ();
}
public static HttpServletResponse GetResponse () {
Return (HttpServletResponse) responses.get ();
}
public static HttpSession getsession () {
Return (HttpSession) getrequest (). GetSession ();
}
public static HttpSession GetSession (Boolean create) {
Return (HttpSession) getrequest (). getsession (create);
}
public void init (Filterconfig config) throws servletexception {
this.config = config;
if (This.config.getInitParameter (charset_encode_param_name)!= null) {
Defaultencode = Config.getinitparameter (charset_encode_param_name);
}
}
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
ServletRequest srequest = Request;
Srequest.setcharacterencoding (Defaultencode);
Requests.set ((httpservletrequest) request);
Responses.set ((httpservletresponse) response);
Chain.dofilter (srequest, response);
}
public void Destroy () {
This.config = null;
}
}
<filter>
<filter-name>common filter</filter-name>
<filter-class>com.test.web.filter.CommonFilter</filter-class>
<init-param>
<param-name>Charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>common filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Later in any program, such as the ordinary JavaBean to make the time
Logger.debug (Commonfilter.getrequest (). Getremotehost ());