The garbled problem that Tomcat produces when processing get and post requests

Source: Internet
Author: User

Original address: Tomcat in the original Phantom when processing get and post requests

Recently has been doing things about the servlet, often garbled, it is annoying, the method of handling garbled sometimes effective, sometimes no effect, today take a time to summarize, in case later this problem.

The usual way to deal with garbled characters is to use:

Request.setcharacterencoding ("GBK")

However, this method only works on forms that are submitted by the Post method, which is why the previous mentions sometimes have effects and sometimes have no effect.

Online data show: Since tomcat5.x started, get and post methods to submit information, Tomcat takes a different way to handle the encoding, for the POST request, Tomcat will still use the encoding set by the Request.setcharacterencoding method to handle, if not set The default iso-8859-1 encoding is used. while get requests are different, Tomcat does not take into account the encoding that is set with the Request.setcharacterencoding method for get requests, and will always use iso-8859-1 encoding , so Tomcat will use ISO-8859-1 to convert the committed bytes into a string.

For this reason, there are three solutions:

1. Change the GET request to a POST request, then you can use the Request.setcharacterencoding method to set the encoding, and use the Request.getparameter method to get the Chinese request parameters directly, this method is simple and clear.

?

2. Without changing the GET request, use the following code in the servlet to get the Chinese request parameter.

? String?name?=?new? String (Request.getparameter ("name"). GetBytes ("Iso-8859-1"),? " GBK ");

?

3. For the Get method, you can also fix it by modifying the Tomcat configuration.

Here's how:

The <Connector?/> tag in conf/server.xml nega uriencoding= "GBK", or UTF-8. Such as:

<connector?port= "8080"? protocol= "http/1.1"? redirectport= "8443"? uriencoding= "GBK"/>

?

For the form submitted by the Post method, the method two above is still valid, the personal recommendation is to use method two, because the code of method two is to take advantage of Java Coding ability, for all Web servers will be effective, The Setcharacterencoding method is supported by the Web server, and not all Web servers will support this method well.

The garbled problem that Tomcat produces when processing get and post requests

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.