Motivition
A netizen once asked me this question:
<% @ Page contentType = "text/html; charset = UTF-8" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
</Head>
<Body>
China
</Body>
</Html>
Why does the "China" page become garbled during operation?
Analysis
Key Step
For the analysis of the above problem, we need to look at the lifecycle of the JSP page request. Generally, we need to go through the following stages:
1. The application server generates a Java file based on the JSP page.
2. The application server calls java.exe to compile the Java file into a class file corresponding to the Servlet.
3. The user's browser requests the Servlet corresponding to the JSP. The Web container starts a thread to execute the Servlet and returns the data to the client browser.
4. The user's IE displays the result to the user based on the returned data.
Key Step Analysis
To better understand the coding problem, we will analyze the problem step by step from the above four links and obtain the final solution based on the analysis results.
1. The application server generates a Java file based on the JSP page.
The application server reads the code of the entire JSP page and writes it to a new JAVA file. Encoding is involved in reading and writing files, how does the app server solve this encoding problem? I studied the source code of the Tomcat application server and found that the pageEncoding parameter in Tomcat is very important. ParserController reads this parameter from the JSP file (if not, read charset from contentType in the first line, and save it. If this parameter is not read, a default PageEncoding parameter is read from JspConfig, if these two parameters are not set, the system will default to the ISO8859-1 encoding to read the original JSP file.
From the above analysis, we have basically understood the encoding method for the application server to read JSP files. Because the Java underlying layer stores characters based on Unicode encoding, when writing files, are output as Unicode encoding.
2. When JDK compiles a Java file into a Class file
You can use the-encoding parameter to specify the source file encoding, which is very important during manual compilation, because this determines the encoding method used by the Java virtual machine to read Java files, however, we can ignore this link in Web applications because the application server can solve this encoding well. Take Tomcat as an example, because the generated java file is a fixed UTF-8 encoding, so Tomcat is also fixed to use UTF-8 encoding to read, through browsing AbstractCatalinaTask can see reader = new InputStreamReader (hconn. getInputStream (), CHARSET); CHARSET = utf-8. Therefore, the application server can be well grasped in this step without leading to coding problems.
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.