Reprinted: indicate http://www.9cbs.net/develop/Article/17/17204.SHTM Tomcat Chinese processing (4):
2) Characters in servlet and other Java files
In this case, the system default encoding method used by the constructed String.
However, the characters obtained from the request in the servlet, if no Request is encoded, then the encoded mode is the character of ISO-8859-1, in the servlet, if there is no Response encoding mode (via setContentType), then The ISO-8859-1 encoding method used by Response.
example:
Import javax.servlet. *;
Import javax.servlet.http. *;
Public class helloworldexample extends httpservlet {
Public void doget (httpservletRequest Request,
Httpservletresponse response
THROWS IOEXCEPTION, ServletException
{
String name = request.getParameter ("name"); / gets Value from Name parameters
Response.setContentType ("text / html"); /// does not set the encoding, at this time, RESPONSE uses ISO-8859-1 encoding
PrintWriter Out = response.getwriter (); // Get a character stream, at this time, the code is ISO-8859-1
Out.println ("");
Out.println ("
");String title = "Hello"; / constructs a string, notice that although it is not specified for the response, the system default encoded in the String constructed in the servlet.
Out.println ("
Out.println (" HEAD>");
Out.println ("
Out.println ("We do you
"); // / Outputs a String to Response encoding a local default, but at this time, the RESPONSE code is ISO-8859-1, so there is garbled
Out.println ("Title IS" Title); Title is the default code of the system, get garbled
Out.println ("
Name IS" Name); / name is Request's Correct display
Out.println (" Body>");
Out.println (" html>");
System.out.println ("Hello"); Since console is the system default code, it is correctly displayed.
System.out.println ("Title IS" Title); / Title is the default encoded, normal display
System.out.println ("Name is" name); /// name is the code of ISO-8859-1, garbled}
}
If we are in response.setContentType ("text / html"); change to: response.setContentType ("text / html; charset = GB2312"); then, the output of IE and the console output are the same.
to sum up:
1. <% @ Page ContentType = "text / html; charset = a"%> If specified, then since all constructs in the JSP (not reference) are not specified, these String encodings It is a.
String from Request If you do not specify a request to encode, he is ISO-8859-1
String obtained from other places is using the original initial encoding, such as getting string from the database, if the code is encoded is B, then the String encoding is b instead of A, nor is the system default.
At this time, if the String of String is not A, then it is likely to display garbled, so the string is first converted to the String of encoding A, and then output.
2. <% @ page contentty = "text / html; charset = a"%> is not specified, then the designated <% @ page contenttype = "text / html; charSet = ISO-8859-1"% >
3. If you perform like Response.setContentType ("text / html; charset = a") in ServelTe; Description Setting the Response's character output stream code is set to a, all the String encoding to be output is to be converted to A, otherwise it will be garbled. of.
The String of Request is the same as the String encoding and JSP, but the String constructed in the servlet java file is the system default encoding. String obtained from the outside in ServelT is the original encoding, such as data obtained from the database encoded to b, is encoded to B, not A, nor is the system default encoding.
Remnant: Although we use Tomcat to explain, other JSPs, servlet engines don't have much way!