js|web| Program | solve | problems | Chinese this time often see someone ask how Chinese is always the number in web development. The reason is simple, because most people use Tomcat servers, and the Tomcat server's default encoding is iso-8859-1 (Western European characters). Is it because the iso-8859-1 (Western European character) code causes what we often see? Resolution
Method One: The simplest is the most frequently 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.
public void Destroy () {
TODO automatically generate method stubs
this.encoding = null;
This.filterconfig = null;
}
Protected String selectencoding (ServletRequest request) {
return (this.encoding);
}
}
and then Web.xml Plus
!--Set Character encoding-->
<filter>
<filter-name> Set Character Encoding </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> Set Character Encoding </filter-name>
<url-pattern>/* </url-pattern>
</filter-mapping>
!--Set Character encoding-->
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 the uriencoding in Tomcat's Server.xml file.
<connector debug= "0" acceptcount= "connectiontimeout=" 20000 "true"
port= "redirectport=" "8443" enablelookups= "false" minsparethreads= "75" maxsparethreads= "
maxthreads= "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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.