Servlet Source file to Class Process
The Servlet Source file is a text file ending with ". java. This section describes the Servlet compilation process and tracks Chinese changes.
Use "javac" to compile the Servlet source file. Javac can contain the "-encoding <Compile-charset>" parameter, which means "interpreting the Serlvet source file with the encoding specified in <Compile-charset> ".
When the source file is compiled, <Compile-charset> is used to interpret all characters, including Chinese and ASCII characters. Then convert the character constant to the Unicode character, and finally convert the Unicode to UTF.
In Servlet, you can also set the CharSet of the output stream. Before outputting the result, call the setContentType method of HttpServletResponse to achieve the same effect as setting <JSP-charset> in Jsp, which is called <Servlet-charset>.
Note: three variables are mentioned in this article: <Jsp-charset>, <Compile-charset>, and <Servlet-charset>. The JSP file is only related to <Jsp-charset>, while <Compile-charset> and <Servlet-charset> are only related to Servlet.
- Import javax. servlet .*;
- Import javax. servlet. http .*;
- Class testServlet extends HttpServlet
- {
- Public void doGet (HttpServletRequest req, HttpServletResponse resp)
- Throws ServletException, java. io. IOException
- {
- Resp. setContentType ("text/html;Charset=GB2312");
- Java. io. PrintWriterOut=Resp. GetWriter ();
- Out. println ("");
- Out. println ("# Chinese #");
- Out. println ("");
- }
- }
This file is also written in UltraEdit for Windows, where the word "Chinese" is saved as "D6 D0 CE C4" GB2312 encoding ).
Start compilation. The following table lists the hexadecimal codes of the "Chinese" characters in the CLASS file when <Compile-charset> is different. <Servlet-charset> does not play any role during compilation. <Servlet-charset> only affects the output of CLASS files. In fact, <Servlet-charset> and <Compile-charset> are used together, this achieves the same effect as <JSP-charset> In the Jsp file, because <Jsp-charset> affects both compilation and CLASS file output.
The transformation process from Servlet Source file to Class in "Chinese"
Equivalent Unicode code in the Class file in the Compile-charset Servlet Source File
GB2312 D6 D0 CE C4
(GB2312) E4 B8 AD E6 96 87 (UTF) \ u4E2D \ u6587 (= "Chinese" in Unicode ")
ISO-8859-1 D6 D0 CE C4
(GB2312) C3 96 C3 90 C3 8E C3 84 (UTF) \ u00D6 \ u00D0 \ u00CE \ u00C4 (one 00 is added before D6 D0 CE C4)
No Default) D6 D0 CE C4 (GB2312) with ISO-8859-1 with ISO-8859-1
The compilation process of a common Java program is exactly the same as that of a Servlet.
Is the Chinese Representation in the CLASS file clearly revealed? OK. Let's take a look at how the CLASS outputs Chinese characters?
- Servlet Context range
- Brief Analysis on the concept of Servlet 3.0 API
- Servlet comments and deployment descriptors
- New Features of Java Servlet and Servlet 3.0
- Potential risks of Servlet and JSP