Three methods for displaying Chinese Characters in web programs in JSP development
Source: Internet
Author: User
Method 1: the simplest and most commonly used method <% @ Page Language = "Java" pageencoding = "GBK" %>
Or <% @ page contenttype = "text/html; charset = GBK";> here, you can use gb2312 or GBK, which is only a string of many characters supported by GBK than gb2312.
This method is used to display Chinese characters on the JSP page.
Method 2: Use a filter Filters are mainly used for form submission. Are the data inserted into the database? . This is also for Tomcat not according to the request specified encoding, or self-made adopts the default encoding mode iso-8859-1 encoding.
Compile a setcharacterencodingfilter class.
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;
Public class setcharacterencodingfilter implements filter {
Protected string encoding = NULL;
Protected filterconfig = NULL;
Protected Boolean ignore = true;
Public void Init (filterconfig) throws servletexception {
This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL)
This. Ignore = true;
Else if (value. inclusignorecase ("true "))
This. Ignore = true;
Else
This. Ignore = false;
}
Public void dofilter (
Servletrequest request, servletresponse response, filterchain chain)
Throws ioexception, servletexception {
// Todo automatically generates method stubs
If (ignore (request. getcharacterencoding () = NULL )){
String encoding = selectencoding (request );
If (encoding! = NULL)
Request. setcharacterencoding (encoding );
}
Chain. dofilter (request, response );
}
Public void destroy (){
// Todo automatically generates method stubs
This. Encoding = NULL;
This. filterconfig = NULL;
}
Protected string selectencoding (servletrequest request ){
Return (this. Encoding );
}
}
Then, add web. xml <! -- 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 -->
There are many benefits to using filters, especially in projects.
And it is more useful when using internationalization, as long as the page specifies <% @ page Language = "Java" pageencoding = "UTF-8" %>, the server displays the correct character set according to the local locale.
Therefore, we recommend that you use filters.
Method 3: Modify uriencoding In the Tomcat server. xml file. <Connector DEBUG = "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 method is mainly used to obtain strings from URLs.
In tomcat5.0 and later versions, the post and get methods are different in encoding. If you get Chinese characters in the URL, what will happen? . But there is no problem in tomcat4.1, because the post and get methods of tomcat4.1 are the same in encoding.
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.