This article describes the filter to set the problem of character encoding, by writing a servlet and configuration web.xml to achieve
This eliminates the need for the code of Pride characters in each JSP page, and the value needs to be encoded in the Web.xml configuration.
Web.xml configuration content is as follows:
<!--character filter;
<filter>
< Filter-name>encodefilter</filter-name>
<filter-class
Test.servlet.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name >encoding</param-name>
< Param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The contents of the filter are as follows:
Package test.servlet;
Import javax.servlet.*;
Import java.io.IOException;
public class Characterencodingfilter implements Filter {
protected String encoding = "UTF-8";
protected Filterconfig filterconfig = null;
public void init (Filterconfig filterconfig) throws Servletexception {
This.filterconfig = Filterconfig;
this.encoding = Filterconfig.getinitparameter ("encoding");
}
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
Conditionally select and set the character encoding to be used
if (request.getcharacterencoding () = null) {
String encoding = selectencoding (request);
if (encoding!= null) {
request.setcharacterencoding (encoding);
}
}
Pass control in to the next filter
Chain.dofilter (request, response);
}
Protected String selectencoding (ServletRequest request) {
return (this.encoding);
}
public void Destroy () {
this.encoding = null;
This.filterconfig = null;
}
}
Go: Character Page Setup mode:
1. pageencoding:<%@ page pageencoding= "UTF-8"%>
JSP page encoding: The encoding of the JSP file itself
2. ContentType: <%@ page contenttype= "text/html; Charset=utf-8 "%>
Web page display encoding: The encoding that the output stream of the JSP displays in the browser
3. html page charset:<meta http-equiv= "Content-type" content= "text/html"; Charset=utf-8 ">
Web page input encoding: input box input font encoding
4. SetCharacterEncoding:request.setCharacterEncoding (), response.setcharacterencoding ()
Request flow for Web server input: Request data for Web server's corresponding browser
5. SetContentType:response.setContentType ()
Response flow for Web server output: Output data for Web-server-appropriate browsers
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/XinVSYuan/archive/2009/02/05/3864853.aspx