Servlet solves parameter garbled problem

Source: Internet
Author: User

Why are garbled characters generated?

Garbled, is due to the server side and the client's encoding inconsistent results. There are two data exchanges during the client-server interaction: The first time, the client initiates the request to the server, the second data exchange, the server responds to the client's request, and returns the processing result to the client.

The premise is that, regardless of whether the server side is accepting data or returning data, if its data encoding format is not specified, then he will encode it by its default "Iso8859-1".  

First look at the client making the request to the server side:

The client's page is encoded to open a page, and when the request is sent over the HTTP protocol to the server, it is encoded to convert the commit data to its corresponding binary number for HTTP transmission. This encoding is specified in the page creation, the most common is to make an HTML file through the <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "> Specifies that the encoding format is UTF8. Then, when the server side accepts this data, it is necessary to decode the binary data by UTF8. If you use other encoding methods, such as GBK to decode, there will be garbled.

Example: Client transfer "Beijing" two words to the server side, if the page opened is utf-8 format, then will be utf8 to the "Beijing" into its corresponding binary number a:1000100111001010 (fabricated), on the server side of the reception, If the UTF8 is not specified to accept this binary stream A, then the server in its default encoding format iso8859-1 to decode A, for different encoding method corresponding to the code table is not the same, the same 1000100111001010,iso8859-1 may not parse out the corresponding characters, So what would it be? or garbled to replace, and then output, which is we see the garbled.

So what we're going to do is unify the encoding format on both sides, and parse the incoming binary data stream in the server-side encoding format of the client page. You can do the following in the Doget () and Dopost () methods of the servlet:

Page submits the form in the Get method:

The Doget () method in the servlet that handles the Get submission method

     Public void doget (httpservletrequest request, httpservletresponse response)             throws servletexception, IOException {                = Request.getparameter ("username");         New String (Username.getbytes ("iso8859-1"), "Utf-8");//the incoming binary data stream is converted to Utf-8 System.out.println by iso8859-1 decoding        (username);    }

For the Dopost () method, the code is as follows

     Public void DoPost (httpservletrequest request, httpservletresponse response)             throws servletexception, IOException {                // solve the garbled problem        in the Post method submission data / /        Request.setcharacterencoding ("Utf-8"); // The decoding method is set to the source page encoding method, this sentence added to the first sentence        of this method String add = Request.getparameter ("Address");        System.out.println (add);           }

Then look at the server side to return the data to the client

The principle is basically with the client to the server side to send requests, to do the same on both sides of the code. The practice is when the server responds, first, set the client corresponding HTTP protocol its data output encoding format for the specified format (if UTF8); the second data to be output is encoded in UTF8 format.

 Public class extends HttpServlet {    publicvoid  doget (httpservletrequest request, HttpServletResponse Response)            throws  servletexception, IOException {                response.setcontenttype ( "Text/html;charset=utf-8"); // setting the HTTP output format        Response.setcharacterencoding ("Utf-8"); // set character encoding format        Response.getwriter (). Write ("Beijing");    

The results of this servlet visit are:

Servlet solves parameter garbled problem

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.