Method One: The simplest is the most used method <%@ page language="java" pageEncoding="GBK" %>
or <%@ page contenttype= "TEXT/HTML;CHARSET=GBK", where you can use gb2312 or GBK, but GBK supports more characters than gb2312.
This method is used for Chinese display in JSP pages.
Method Two: Use the filter
Filters are used primarily for form submissions, and data is inserted into the database? Resolution This should also be the encoding that Tomcat does not specify for the request, or the default encoding for ISO-8859-1 encoding.
Write a Setcharacterencodingfilter class.
importjava.io.IOException;
Importjavax.servlet.Filter;
Importjavax.servlet.FilterChain;
Importjavax.servlet.FilterConfig;
Importjavax.servlet.ServletException;
Importjavax.servlet.ServletRequest;
Importjavax.servlet.ServletResponse;
publicclasssetcharacterencodingfilterimplementsfilter{
Protectedstringencoding=null;
Protectedfilterconfigfilterconfig=null;
Protectedbooleanignore=true;
Publicvoidinit (filterconfigfilterconfig) throwsservletexception{
This.filterconfig=filterconfig;
This.encoding=filterconfig.getinitparameter ("encoding");
Stringvalue=filterconfig.getinitparameter ("ignore");
if (value==null)
This.ignore=true;
ElseIf (Value.equalsignorecase ("true"))
This.ignore=true;
Else
This.ignore=false;
}
Publicvoiddofilter (
Servletrequestrequest,servletresponseresponse,filterchainchain)
throwsioexception,servletexception{
//todo Automatic Generation method SaveThe root
if (Ignore (request.getcharacterencoding () ==null)) {
stringencoding=selectencoding (request);
if (encoding!=null)
request.setcharacterencoding (encoding);
}
Chain.dofilter (request,response);
}
Publicvoiddestroy () {
//todo automatically generate method stubs
This.encoding=null;
This.filterconfig=null;
}
Protectedstringselectencoding (servletrequestrequest) {
return (this.encoding);
}
}
and then Web.xml Plus
<!--SetCharacterEncoding-->
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>com.struts.common.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--SetCharacterEncoding-->
The benefits of using filters are many, especially among projects.
And more useful when using internationalization, as long as the page specifies <%@ page language= "java" pageencoding= "UTF-8"%>, the server will display the correct character set according to the local locale.
So I particularly recommend using filters.
Method Three: Modify Tomcat's Server.xml file uriencoding
<Connectordebug="0"acceptCount="100"connectionTimeout="20000"disableUploadTimeout="true"
port="80"redirectPort="8443"enableLookups="false"minSpareThreads="25"maxSpareThreads="75"
maxThreads="150"maxPostSize="0"URIEncoding="GBK">
</Connector>
This approach is primarily for getting strings from URLs.
In tomcat5.0 and above versions, the Post and get methods differ in the processing of encodings. If you get Chinese in the URL, it will appear? Resolution However, there is no problem with the tomcat4.1 version, because the tomcat4.1 post and get methods are the same when coding.