Java Web Development Summary (iii)--request receiving form submission Chinese parameter garbled problem

Source: Internet
Author: User
Tags java web

1, post the form of the Chinese parameter garbled problem

<%@ page language="Java"import="java.util.*"pageencoding="UTF-8"%><! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en">"<%=request.getcontextpath ()%>/servlet/requestdemo04"Method="Post">User name:<input type="text"Name="UserName"/> <input type="Submit"Value="Post mode submission Form"> </form> </body>

Garbled, because the server and client communication is inconsistent with the encoding, so the solution is to set up a unified code between the client and the server, followed by this encoding for data transmission and reception.

Because the client transmits the form data to the server side with the UTF-8 character encoding, the server also needs to be set to receive UTF-8 character encoding, and to do so, the server can directly use the inherited from the ServletRequest interface. Setcharacterencoding (CharSet) "Method for uniform encoding settings. The modified code is as follows:

 Public void DoPost (httpservletrequest request, httpservletresponse response)             throws servletexception, IOException {        /**         * Client is transmitting data to server-side with UTF-8 encoding, So you need to set the server side to UTF-8 encoding to receive, otherwise the Chinese data will be         garbled *        /request.setcharacterencoding ("UTF-8" );         = Request.getparameter ("UserName");        System.out.println ("userName:" +userName);}

Use request.setcharacterencoding ("UTF-8"); After setting up the server to receive data in UTF-8 encoding, there is no Chinese garbled problem at this time.

2, to get the form to submit forms Chinese parameter garbled problem

Import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >          Name:<input type=" text " Name= "name"/>          <input type= "Submit" value= "Get Way Submit Form" >       </form>  </body>

For the Chinese data transmitted by get, through Request.setcharacterencoding ("UTF-8"), this way is not able to solve the Chinese garbled problem.

For data transmitted in a Get mode, the request is not valid even if it is set to receive data in the specified encoding (as to why it is not valid I do not understand), the default is to use the Iso8859-1 character encoding to receive data, the client to UTF-8 encoding to transmit data to the server side, The server-side Request object uses the Iso8859-1 character encoding to receive the data, the server and client communication encoding inconsistent so it will produce garbled Chinese. Workaround: After receiving the data, get the request object to Iso8859-1 character encoding to receive the original data byte array, and then through the byte array with the specified encoding to build a string, to solve the garbled problem.

 Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {/*** * For data transmitted in Get mode, the request is invalid even if it is set to receive data in the specified encoding, the default is to use the Iso8859-1 character encoding to receive the data .*/String name= Request.getparameter ("name");//Receive DataName =NewString (Name.getbytes ("iso8859-1"), "UTF-8");//gets the byte array of the raw data received by the request object in Iso8859-1 character encoding, and then constructs the string with the specified encoding through a byte array to solve the garbled problemSystem.out.println ("Name:" +name); }

3, in the form of hyperlinks to pass the Chinese parameter garbled problem

The client wants to transfer the data to the server, either through form submission or by adding parameters to the hyperlink, for example:

<a href= "${pagecontext.request.contextpath}/servlet/requestdemo05?username=gacl&name= 徐达沛" > Click </a >

Click the hyperlink, the data is transferred to the server in a get, so the reception of Chinese data will also produce Chinese garbled problem, and the way to solve the Chinese garbled problem with the above to submit the form Chinese data garbled processing problems in the same way.

String name = Request.getparameter ("name"=new String (name.getbytes ("iso8859-1"), "UTF-8");

In addition, it is necessary to mention that the URL after the address if the Chinese data, then the Chinese parameter is best to use URL encoding for processing , as follows:

<a href= "${pagecontext.request.contextpath}/servlet/requestdemo05?username=gacl&name=<%= Urlencoder.encode ("Xu Da", "UTF-8")%> "> Click </a>

4, the submission of Chinese data garbled problem summary

1, if the submission method is post, want to not garbled, only need to set the Request object encoding on the server side, the client is submitted by which encoding, the server-side Request object is received by the corresponding code, such as the client is submitted by UTF-8 encoding, Then the server-side Request object is received in UTF-8 encoding (request.setcharacterencoding ("UTF-8"))

2, if the submission method is get, set the encoding of the request object is not valid, the request object or the default iso8859-1 encoding to receive data, so to not garbled, only after receiving data and then manually converted, the steps are as follows:

1). Get the data from the client submission, get the garbled string, data= "??? È????? "

   String data = Request.getparameter ("paramname");

2). Find the Iso8859-1 Code table and get a byte array of raw data submitted by the client

   byte[] Source = data.getbytes ("iso8859-1");

3). Build a string with the specified encoding through a byte array to resolve garbled characters

   data = new String (source, "UTF-8");

Constructs a string with the specified encoding through a byte array, where the encoding specified is based on the character encoding used when the client submits the data, and if it is GB2312, it is set to data = new string (Source, " GB2312 "), if it is UTF-8, then set to data = new String (source," UTF-8 ")

Java Web Development Summary (iii)--request receiving form submission Chinese parameter garbled problem

Related Article

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.