Completely solve Tomcat 5.0.19 Chinese garbled

Source: Internet
Author: User

One, include the page garbled

Phenomenon: Include in the page appears garbled, other pages normal.

Cause: Tomcat uses the ISO-8859-1 encoding by default, but sometimes Tomcat does not correctly parse the included file according to the encoding of the outer. jsp file when included, causing the Chinese garbled in the file include.

Solution: There are many solutions here, but the fundamental solution to our Chinese environment is to modify the core default encoding of Tomcat 5.0.19 from Iso-8859-1 to GBK. You can download the modified jar file at the following address.

Jasper-compiler.jar,jasper-runtime.jar is located under/common/lib, and the other is under/server/lib, replacing the original. jar file with the new.

Second, the submitted data garbled

Phenomenon: The data submitted by the form is garbled.

Reason: The reason is not clear. It is possible that Tomcat, after receiving the request, has not been able to encode the correct encoding according to the information in request.

Workaround: You can add a filter that sets the character set.

Package Filters
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;
Import javax.servlet.UnavailableException;  
public class Setcharacterencodingfilter implements Filter {
protected String encoding = NULL;
protected Filterconfig filterconfig = null;
Protected Boolean ignore = true;   
public void Destroy () {
this.encoding = null;
This.filterconfig = null;
}
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain)
throws Ioexc Eption, Servletexception {
//conditionally Select and set character encoding to is used
if (Ignore | | (request.getcharacterencoding () = null)) {
String encoding = selectencoding (request);
if (encoding!= null)
Request.setcharacterencoding (encoding);
}
//pass control on to the next filter
Chain.dofilter (request, response);
}  
public void init (Filterconfig 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.equalsignorecase ("true"))
This.ignore = true;
 else if (value.equalsignorecase ("yes"))
This.ignore = true;
else
This.ignore = false;
}
Protected String selectencoding (ServletRequest request) {
return (this.encoding);
}
}

Configure Web.xml

<filter>
 <filter-name>Set Character Encoding</filter-name>
 <filter-class>filters.SetCharacterEncodingFilter</filter-class>
 <init-param>
  <param-name>encoding</param-name>
  <param-value>GBK</param-value>
 </init-param>
</filter>
<filter-mapping>
 <filter-name>Set Character Encoding</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

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.