On the problem of garbled characters in Sevlet file processing and the processing method

Source: Internet
Author: User

Before doing the web big job (paper sharing system), the basic function of the implementation of relatively smooth, but garbled problem has been troubling me, through a series of attempts and access to relevant information, summed up the following situations and related processing methods, I hope to meet the same problem you useful.

One, the form is submitted garbled:

In the form submission, often submit some Chinese, naturally can not avoid the case of Chinese garbled, for the form, there are two ways to submit: Get and post submission method. Each method has a different solution, garbled, because the GET request, the data passed to the server is appended to the URL address, while the POST request, the data passed to the server is passed as part of the request body to the server. This results in a different way of handling the garbled characters they produce.

1, the client's GET request

For the client's get request, the server-side processing to avoid garbled, need to use the string type of the constructor, one of the constructors is to use the specified encoding method to decode, generally use "UTF-8" way. As long as the requested parameters are re-constructed into a string on the server side. As shown below:

String stuname = Request.getparameter ("Stuname");

String str = new String (Stuname.getbytes ("iso-8859-1"), "Utf-8")

After construction, when the client enters Chinese, and the form has a GET request, Str becomes Chinese. If the request parameter is more, it is best to encapsulate it as a tool class:

public class Myutil

{

public static string getnewstring (String str) throws Unsupportedencodingexception

{

return new String (Str.getbytes ("iso-8859-1"), "UTF-8");

}

}

String stuname= myutil.getnewstring (Request.getparameter ("Stuname"));

2, the client's post request

For the client's post request, the problem of handling garbled characters is relatively simple, as long as the code in the request to modify the line. As long as the requested data is set to "UTF-8" at the beginning of the server-side, enter the following statement:

Request. Setcharacterencoding ("UTF-8");

Second, the hyperlink appears garbled

In the Web development, quite a lot of time is through the hyperlink to pass the Chinese parameters, which will also lead to garbled in the display, for the hyperlink, it is actually sent to the server side of a request, and it sends the request is a GET request, so for the garbled of the hyperlink, It handles garbled ways and the form's get requests appear garbled in the same way.

String stuname= myutil.getnewstring (Request.getparameter ("Stuname"));

Third, the redirection is garbled

Sometimes write on the response Sendredirect () method to redirect also garbled, when the redirect is actually sent to the server a request, so the method of solving garbled is the same as above.

Four, return the browser display garbled characters

In servlet programming, it is often necessary to return some information through the response object to the browser, to our client, while we display the Chinese on the server side, but the response to the client browser is garbled, mainly due to the response object's getwriter () The PrintWriter object returned by the method uses the "Iso-8859-1" character set encoding to convert the Unicode string to a byte array by default, since the iso8859-1 character set contains no Chinese characters at all. So Java in the conversion when the invalid character encoding output to the client, so there is garbled, for this servletresponse interface defines the setcharacterencoding, setContentType methods to specify the character set encoding used by the PrintWriter object returned by the Getwriter method, so we set the values of these methods before calling the Getwriter method in the Write Servlet program. In order to prevent garbled, we often write the following two statements together:

Response.setcontenttype ("Text/html;charset=utf-8");

Response. Setcharacterencoding ("UTF-8");

As long as you write the servlet file that contains the information that responds to the client, write these two words. It is best to write the second sentence because it has a high priority, and its setting will overwrite the set of character encodings such as setContentType.

V. Modifying the Tomcat encoding

In the above-mentioned get request caused by the garbled problem, there is also a solution, we often use Tomcat as a container to run the servlet and JSP, and tomcat internal default encoding is iso-8859-1, so for GET request way, its passed data (URI) is appended to the resource accessed, its encoding is tomcat default, if you modify the encoding of the URI, then for all GET request way is not garbled, including the above-mentioned redirect and hyperlink, Locate the port where you modified tomcat in the Tomcat configuration file Server.xml, add the uriencoding attribute inside it, set it to the same value as the encoding set in your project, all of which are UTF-8. As shown below:

<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443" URIEncoding= "UTF-8"/ >

On the problem of garbled characters in Sevlet file processing and the processing method

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.