1 at first, the servlet did not process the Request Encoding, that is, no
Request. setcharacterencoding ("UTF-8 ");
In this case, the submitted Chinese characters are displayed on the page after servlet processing.
However, in Serlvet, system. out. the println () output is garbled, which indicates that the strings used in Java code are actually incorrect and cannot be effectively compared and judged and processed. 2 add in Servlet
Request. setcharacterencoding ("UTF-8 ");
At this time, the output of system. Out. println is normal, but the page is garbled.
This indicates that the strings processed in the Java code are correct. 3. Add the strings to the servlet.
Response. setcontenttype ("text/html; charset = UTF-8 ");
The theory is okay:
1. Strings processed by encodinguri should be treated as UTF-8 encoded.
2 Serlvet in the default (request. getcharacterencoding () = NULL) that request encoding is "ISO-8859-1"
3. the browser determines which encoding is used based on the contenttype in the httpresponse header when processing server messages. Explanation:
In the past, there was a filter for processing requests in the framework. There was only one valid code, that is
Request. setcharacterencoding ("UTF-8 ");
However, our response is always done through struts action forward to JSP, and JSP will certainly have <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%>
In fact, it is equivalent to the response in sevlet. setcontenttype ("text/html; charset = UTF-8"); Note: according to personal understanding (according to JSP standard description, not explicitly stated) pageencoding = "UTF-8" is to tell the JSP Container which character set is used to interpret JSP files when converting JSP into Java Servlet code. Postscript: Character Set, encoding is an international problem and is a basic problem of Java. It may be too basic to be forgotten by programmers. However, if the underlying problem is not well handled, it will cause a lot of trouble.
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