Jsp/servlet-----CharSet, pageencoding difference

Source: Internet
Author: User
Tags recode

First,the role of several encodings in Jsp/servlet
InJsp/servletin a4a PlaceYou can set the encoding (below), where the first two can be used only forJSP, the latter two can be used toJSPand theServletIn
1,pageencoding= "UTF-8": SetJSPcompiled intoServletthe encoding used when using the.
JspOn the server is to be compiled firstServletthe. pageencoding= "UTF-8"The role is to tellJSPthe compiler willJSPfiles are compiled intoServletthe encoding used when using the. In general, theJSPinternally defined string (directly in theJSPRather than the data submitted by the browser, many are caused by this parameter setting error. For example, yourJSPfiles are inGBKsaved for the encoding, while theJSPspecified inpageencoding= "UTF-8", it will causeJSPthe internally defined string is garbled.
In addition, this parameter also has a function, that is, theJSPnot specified inContentTypeparameter, nor does it useresponse.setcharacterencodingmethod, specifies the encoding to recode the server response


2.contenttype= "Text/html;charset=utf-8": Specifies the encoding to re-encode the server response.
when you do not use response.setcharacterencoding method, you use this parameter to specify the encoding to recode the server response.


3, request.setcharacterencoding ("UTF-8"): Sets the encoding to re-encode the client request.
This method is used to specify the encoding to use when the data sent by the browser is re-encoded (or decoded).


4,response.setcharacterencoding ("UTF-8"): Specifies the encoding to recode the server response.
The encoding is used by the server to re-encode data before it is sent to the browser.




Second,BrowserHow toencode the data that is received and sent?
Response.setcharacterencoding ("UTF-8")The function is to specify the encoding to recode the server response. Meanwhile, the browser is also based onThis parameter encodes (or is called decoding) the data it receives. Can do an experiment, inJSPset inresponse.setcharacterencoding ("UTF-8"), inIEwhen the page is displayed in theIEthe menu-- "View"-- "Coding"can be seen in thethe Unicode(UTF-8)"。
when the browser sends the data, theURLand the parameters will beURLencoding, the Chinese in the parameter, the browser is also to makeresponse.setcharacterencodingparameters to doURLEncoding of.
The encoding used by the browser to receive server data and send data to the server is the same, by default the response.setcharacterencoding of the JSP page Parameters (or contentType and  pageencoding parameter), which we call the browser code 。 Of course, inIEYou can modify the browser encoding in theIEthe menu-- "View"-- "Coding, but typically, modifying this parameter causes garbled characters to appear in the correct page.



Third,ServerHow toReceive and sendof thedata is encoded?
1,For sending data, the server followsresponse.setcharacterencoding-contenttype-pageencodingthe data to be sent is encoded in the order of precedence.
2,For receiving data, there are three cases (the browser directly usesURLthe submitted data,Use the Get of the formdata submitted by the method, the form'sPOSTmethod of submitting data)。 VariousWEBThe server does not treat these three ways in the same wayTomcat5.0as an example. Regardless of which method is submitted, if the parameter contains Chinese, the browser will use the current browser encoding toURLencoding.
for the formPOSTData , as long as the data is received in theJSPthe correctrequest.setcharacterencodingparameter, which will encode the encoding of the client request to the browser code(By default, the browser encoding is the one that should be requested.)JSPOn the pageresponse.setcharacterencodingSet the value), the correct parameter encoding can be obtained.
forURLthe data submitted and the formGETThe data that is submitted in theJSPset inrequest.setcharacterencodingparameter is not possible because theTomcat5.0, by default, use theiso-8859-1theURLthe data submitted and the formGETdata to be re-encoded (decoded) without using this parameter toURLthe data submitted and the formGETre-encode (decode) the data that was submitted by the method. To resolve this issue, you shouldTomcatthe configuration file(Server.xml) ofConnectorLabel SettingsUsebodyencodingforuri(Indicates whether to userequest.setcharacterencodingparameter PairsURLthe data submitted and the formGETTo re-encode the data submitted by thefalse; Tomcat4.0the default istrue) oruriencoding(Specify for allGETmethod requests (includingURLthe data submitted and the formGETdata) for uniform recoding (decoding) encodingProperties




Four,How to prevent Chinese garbled characters
1, the best unified code for the same application, recommended forUTF-8
2, correct settingsJSPof thepageencodingParameters
3, in all theJsp/servletset incontenttype= "Text/html;charset=utf-8"orresponse.setcharacterencoding ("UTF-8"), thereby indirectly implementing the settings for the browser encoding.
4, for requests, available filters or in eachJsp/servletset inrequest.setcharacterencoding ("UTF-8"). Also, to modifyTomcatThe default configuration, recommendedSet usebodyencodingforuri="True "(Request.setcharacterencoding is set first when getting parameter values("UTF-8")), you can also set uriencoding="UTF-8 "(may affect other applications, it is not recommended)

Jsp/servlet-----CharSet, pageencoding differences

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.