Solve the problem of Chinese Characters in JSP web programs

Source: Internet
Author: User
During this time, I often see people asking how Chinese is always used in Web development? . The reason is actually very simple, because we mostly use Tomcat server, and Tomcat server default code for iso-8859-1 (Western European characters ). Because of the iso-8859-1 (Western European character) encoding that we often see? .

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.
 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.