Java Web Chinese garbled processing note

Source: Internet
Author: User

One, the occurrence of Chinese garbled in the case of the following three kinds:

1. Two types of submissions for form form are garbled in Chinese

①get Submit ②post Submission

2. Hyperlinks (essentially get commits) appear garbled in Chinese

3, Sendredirect (garbled)

Second, the cause of garbled characters:

Browser encoding Utf-8 sending a request to the server will result in inconsistent encoding
So in the sending request is the first definition of the code, it will be a good solution to the problem of coding

Three, to solve garbled several simple ways:

Post method submission: The solution is as follows: Request.setcharacterencoding ("Utf-8");

The Get method uses this method still does not solve the problem is still garbled, for Get submit the processing method used

Encapsulation becomes a tool class to handle:

 Public class mytool{    publicstatic  string getnewstring (String str) {        string newstring ="";         Try {        newstringnew String (str.getbytes ("iso-8859-1"), "Utf-8");        } Catch (Exception e) {            e.printstacktrace ();        }         return newstring;}    }

Invoking the tool class:

String u = request.getparameter ("username");
mytool.getnewstring (U);

The way hyperlinks are submitted is the same as get commit processing

Four, browser-compatible version information issues, if IE6 or the following version, the second to third type will appear garbled

Hyperlink garbled and Sendredirect garbled
String Info=java.net.urlencoder encode ("hello. jpg", "utf-8");
<a href= "" + info> Test </a>
Response.sendredirect ("servlet address?") Username= "+ info);

Postscript:

On the server side is Chinese, in the response, but also to consider whether the browser is displayed correctly
Response.setcharacterencoding ("Utf-8");
Response.setcontenttype ("Text/html;charset=utf-8")

Java Web Chinese garbled processing note

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.