1. Before compiling the source file, the Java compiler converts the source file to unicode encoding. For this reason, during compilation, we must "tell" the compiler the correct encoding method used by the source file.
For example, our source file is saved as a UTF-8, while the compiler saves it as a GBK method during compilation, in this way, the compiler will convert the source file according to the GBK-> unicode encoding conversion method and then compile the source file. Of course, errors will occur, in fact, the compiler should convert the source files according to the UTF-8-> unicode encoding conversion method.
A. for console programs, the compiler regards the source file as encoded by the system's default encoding type (the default encoding type of the system depends on the configuration in the control panel settings, win2k is usually GBK), you can also use the-encoding parameter to set, such as: javac-encoding UTF-8, in this way, the compiler will regard the source file as being encoded by a UTF-8 (this only tells the encoding type of the compiler source file, rather than transcode the source file ). On platforms of various languages, as long as the encoding specifies the same encoding method as the source file during compilation, there will be no international problem.
B. for JSP, the compiler will determine the encoding method used by the JSP file based on the set character set, and then convert it to Unicode for compilation. If not specified in JSP, the compiler will regard the JSP file as saved according to the system's default encoding. In jsp2.0, a new instruction is added to notify the compiler of the encoding method used by the source file.
2. When processing input and output, note that the encoding type of the input and output streams is the same as that used when the user input and output devices are displayed.
Because JRE encodes the input or output content when processing the input and output, the input is converted to Unicode before being sent, therefore, we must correctly match the encoding method of the actual input content and inform the JRE of the encoding method. For the output, Unicode will be converted to other encoding before sending the program, therefore, we must correctly match the encoding method used when the output device displays and inform the JRE of the encoding method.
For example, set the input stream encoding in the program to new inputstreamreader (system. in, "gb2312"). After the program runs, the user uses the traditional Chinese Input Method and the big5 encoding content, in this way, the JRE converts the content of big5 encoding to gb2312-> unicode encoding. In this way, the converted result is obviously not the content you want to input.
By default, JRE uses the input and output content as encoded by default.
3. In servlet, apart from being sure to "tell" The Compiler which encoding method the source file uses is correct, note that the encoding formats of the actually submitted URL data and form data are the same as those stated in the request.
When the client browser submits data through forms and URLs, the container and JVM will regard the data in the request as encoded according to the encoding method stated in the request, convert the data from this encoding method to Unicode before sending it to the servlet (in fact, the container will convert the data in the request to an intermediate encoding method, depending on the container configuration, then, it is converted from this intermediate mode to Unicode by JVM. Generally, this intermediate format is ISO ). The Unicode data output by Servlet is converted by the container according to the encoding method stated in response, and then sent to the client browser.
When receiving client input, request. setcharacterencoding () is used to declare the encoding method of the data in the request.
When outputting data to the client, use response. setcontenttype ("text/html; charset ="); declare the encoding method of the response data to inform the browser of the encoding method used for display.
4. in JSP, the JSP is compiled as a servlet by the JSP compiler, so the situation is the same as that in servlet.
The two JSP commands declare the encoding method of the request and response.
To ensure that the encoding method of the URL parameter or data in the form is the same as the declared encoding method, and then notify the JSP compiler of the encoding method used by the JSP file and the characters contained in the file, to solve the issue of JSP character encoding.
Here is a specific example:
Because of browser support for UTF-8, we can easily achieve the goal of dealing with internationalization and character encoding problems by using Unicode encoding in source files, requests, and responses.
Take tomcat4.1.2 as an example. The process is as follows:
1. When compiling a JSP page: add a line to the top of each JSP page:
When editing a JSP page, make sure that the JSP file is saved in Unicode mode. Currently, almost all editors can save the file in Unicode encoding or convert the file content to Unicode.
2. Add a characterencoding class setcharacterencodingfilter. Java to declare the request;
The main function of setcharacterencodingfilter is to declare the encoding when the request is submitted to the server from the page as the desired encoding, and call the Request Method setcharacterencoding (string encoding) so that when the request is sent from the client. encoding configured in XML (which can be discussed in the second point) to encode the submitted data.
3. Modify the Web. xml file and configure a filter to filter all URL requests. The class in step 2 declares that the encoding type of all URL requests is not UTF-8.
Add the following section to the Web. xml file:
Set character encoding Org. Kyle. Web. sample. setcharacterencodingfilter
Encoding
UTF-8
Set character encoding /* |
In the above text, "org. kyle. web. sample. setcharacterencodingfilter specifies the class location in step 2, The UTF-8 specifies the encoding type of the request we want to declare, "/*" specifies the applicability of the filter (all URL requests here ).
Pay attention to the following two issues:
1: The servlet version must support the request. setcharacterencoding (string encoding) method, that is, serlvert2.3 or later.
2: The current code page attribute set in the control panel area must be set to "936 (GBK)", if it is "437 (OEM-United States) "It processes text in 8-Bit mode, and Chinese and Japanese in 16-Bit mode. Therefore, during display and processing, the first eight digits of Chinese characters are cut off, which leads to garbled characters.
Appendix: setcharacterencodingfilter source file
Package org. Kyle. Web. sample;
Import java. Io. ioexception;
Import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;
Import javax. servlet. unavailableexception;
Public class setcharacterencodingfilter implements Filter
{
/**
* The default character encoding to set for requests that pass through
* This filter.
*/
Protected string encoding = NULL;
/**
* The filter configuration object we are associated with. If this value
* Is null, this filter instance is not currently configured.
*/
Protected filterconfig = NULL;
/**
* Shoshould a character encoding specified by the client be ignored?
*/
Protected Boolean ignore = true;
/**
* Take this filter out of service.
*/
Public void destroy ()
{
This. Encoding = NULL;
This. filterconfig = NULL;
}
/**
* Select and set (if specified) the character encoding TO BE USED
* Interpret Request Parameters for this request.
*
* @ Param request the Servlet request we are processing
* @ Param result the servlet response we are creating
* @ Param chain the filter chain we are processing
*
* @ Exception ioexception if an input/output error occurs
* @ Exception servletexception if a servlet error occurs
*/
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain)
Throws ioexception, servletexception
{
// Conditionally select and set the character encoding to be used
If (ignore | (request. getcharacterencoding () = NULL ))
{
String encoding = selectencoding (request );
If (encoding! = NULL)
Request. setcharacterencoding (encoding );
}
// Pass control on to the next Filter
Chain. dofilter (request, response );
}
/**
* Place this filter into service.
*
* @ Param filterconfig the filter configuration object
*
* Encoding
* UTF-8
*
*/
Public void Init (filterconfig) throws servletexception
{
This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL)
This. Ignore = true;
Else if (value. inclusignorecase ("true "))
This. Ignore = true;
Else if (value. inclusignorecase ("yes "))
This. Ignore = true;
Else
This. Ignore = false;
}
/**
* Select an appropriate character encoding to be used, based on
* Characteristics of the current request and/or filter Initialization
* Parameters. If no character encoding shoshould be set, return
* Null.
*
* The default implementation unconditionally returns the value configured
* By the encoding initialization parameter for this
* Filter.
*
* @ Param request the Servlet request we are processing
*/
Protected string selectencoding (servletrequest request)
{
Return (this. Encoding );
}
}
|