Servlet Source File Transfer

Source: Internet
Author: User

The Servlet Source file is a text file ending with ". Java. We will discuss the Servlet compilation process and track the Chinese changes.

Use "Javac" to compile the Servlet source file. Javac can contain the "-encoding" parameter, which means "interpreting Servlet Source files with the encoding specified in <Compile-charset> ".

During compilation, the source file is used to interpret all characters, including Chinese and ASCII characters. Then convert the character constant into a Unicode character. Finally, convert Unicode to UTF.

In Servlet, you can also set the CharSet of the output stream. Before the output result, call the setContent Type method of HttpServletResponse to achieve the same effect as that set in JSP.
Note: three variables are mentioned in this article:, and. The JSP file is related only to Servlet.
See the following example:

 
 
  1. Import Javax. servlet .*;
  2. Import Javax. servlet. http .*;
  3. Class testServlet extends HttpServlet
  4. {
  5. Public void doGet (HttpServletRequest req, HttpServletResponse resp)
  6. Throws ServletException, Java. io. IOException
  7. {
  8. Resp. setContentType ("text/html;Charset=GB2312");
  9. Java. io. PrintWriterOut=Resp. GetWriter ();
  10. Out. println ("");
  11. Out. println ("# Chinese #");
  12. Out. println ("");
  13. }
  14. }

This file is also written in UltraEdit for Windows. The "Chinese" character is saved as the byte stream "D6 D0 CE C4" GB2312 encoding ).

Start compilation. The hexadecimal code of the Chinese character in the Class file. It does not play any role in the compilation process. It only affects the output of the Class file. It can be said that the combination achieves the same effect as that in the JSP file, because it affects both the compilation process and the output of the Class file.

Transition from Servlet Source file to Class

Note: The compilation process of a common Java program is exactly the same as that of a Servlet.
As of now, is the process of changing the Chinese content from the JSP or Servlet Source file to the Class file clearly revealed? OK. Let's take a look at how the Chinese characters in the Class file are output?

Class: Output string

Class files are a storage carrier of Java programs. When the Class file is executed by a virtual machine, read the content in the Class file into the memory through readUTF. The string is represented as Unicode encoding in the memory. When the content in the memory needs to be output to another program or peripheral device (such as a terminal), the problem arises (for simplicity, call "other programs or peripheral devices" as "output objects ").

1. If the output object can process Unicode characters, everything is simple. You only need to pass the Unicode characters directly to the output object.

2. The fact is that most output objects cannot directly process Unicode and they can only process ISO8859-1 and GB2312. When outputting a string to an output object, a certain amount of conversion is required.

Take a look at the example below, given a four-character Unicode string "00D6 00D0 00CE 00C4", if output to a program that can only recognize "ISO8859-1, then, remove the previous "00" to obtain the destination string "D6 D0 CE C4 ". If we output them to the GB2312 program, the result may be a lot of garbled characters. Because it may not exist in GB2312 or possibly) if the correspondence between the character and 00D6 does not match, 0x3f, that is, question mark, will be obtained, because 00D6 and other characters are too high, it is estimated that they are also some special characters. The real Chinese character encoding starts from 4E00 ).

When the same Unicode character is output to an object of different encoding, the result is different. Of course, one of these is the expected result. For output objects that can process Chinese characters, the input content (that is, the content output by the Java program) is a meaningful Chinese string encoded Based on GB2312.

In the example of the Servlet Source File above, "D6 D0 CE C4" should be what we want. When "D6 D0 CE C4" is output to IE, you can use the "simplified Chinese" method to view the "Chinese" clearly.

  1. Analysis of Servlet Web Applications
  2. Install Servlets and JSP
  3. Running Environment of Servlets and JSP-JRun
  4. Getting started with the Servlets Engine
  5. Introduction to Java Servlets and CGI programs

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.