Java Web application Request parameter encoding

Source: Internet
Author: User

We know that when the spelling to jump in the URL contains Chinese, generally need to transcode processing, to the server side to properly receive and processing, which is the client and server-side communication to the data format specification.

Generally, using encodeuricomponent or encodeURI to handle the Chinese parameter (the difference between the two, and the difference between and escape), after encoding, on the client, become the ISO-8859-1 data format, generally displayed on the URL is% XX in this form.

var url = "hello.do?person=" + encodeuricomponent ("xiaoming"= URL;

"Xiao Ming" after encodeURIComponent became "%e5%b0%8f%e6%98%8e".



To the server, the server receives the request parameters and then handles them accordingly.

Different middleware, for the processing of HTTP request parameters, is generally responsible for receiving parameter values, and passed to the application server (when the application request parameter values).

In order to deal with the different situations and the convenience of internal processing, the middleware may do some processing, such as encoding into a specific format, between the initial receiving and final delivery to the application server.

In Java, it is common to use getparameter to get the parameters passed by the client (the difference between Ajax and jump, and the difference from GetAttribute, getattribute is the transfer of data between the server and the server), usually in the form of:

String person = request.getparameter ("persion");

So, the encoding factor is taken into account in this way, and in fact, if the HTTP request encoding format is not specified using middleware, the default HTTP request encoding format of the corresponding middleware is used. The way to set the HTTP request encoding format is different for the Get mode and post mode requests.

As I understand it, the middleware HTTP default request encoding refers to what encoding it by default identifies data that is passed from the client.

For post, generally, before getting all the request parameters,调用request.setCharacterEncoding("ISO-8859-1")即可指定post内容编码格式,然后再根据后面需要,转换成需要的编码格式的串。

Request.setcharacterencoding ("iso-8859-1"= request.getparameter ("person"new String ( Person.getbytes ("Iso-8859-1"), "UTF-8");

For Get mode,

Tomcat default receive HTTP request parameter encoding format is iso-8859-1, of course, can also be modified in the configuration file to other, location:server.xml file, the corresponding port of the connector properties: uriencoding= UTF-8 (generally not modified, if it is necessary to receive as iso-8859-1, in the Code of Uniform processing encoding)

<connector port= "8080" maxhttpheadersize= "8192"
maxthreads= "minsparethreads=" "maxsparethreads=" uriencoding=utf-8
Enablelookups= "false" redirectport= "8443" acceptcount= "100"
connectiontimeout= "20000" disableuploadtimeout= "true"/>

Jetty the default receive HTTP request parameter encoding format is UTF8, typically configured in the JVM startup parameters, such as:

-dorg.mortbay.util.uri.charset=iso8859-1

The Get method actually requests the parameters, the code is as follows:

string person = request.getparameter ("person"new String (person.getbytes ("iso-8859-1"), "UTF-8");

Java Web application Request parameter encoding

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.