When using response to output Chinese to the client browser in a servlet, there are sometimes garbled problems, summarized as follows:
Response output stream has two kinds, one is the output of the byte stream, the first is the character stream output.
One, the output of the byte stream:
1. The default encoding output wood is garbled
2. Response the SetHeader method to set the encoding utf-8, no garbled
3. Response the setContentType method to set the encoding utf-8, no garbled
4. Output numbers are recommended as a string output
Second, the character stream output:
1. The default check Iso-8859-1 code table (required by the Sun's servlet specification), the client displays garbled
2. Response the SetHeader method to set the encoding utf-8, no garbled
3. Response the setContentType method to set the encoding utf-8, no garbled
The byte stream is output with the default encoding:
1 Public voiddoget (httpservletrequest request, httpservletresponse response)2 throwsservletexception, IOException {3 //use byte stream to output Chinese data to client with default encoding, wood garbled4Response.setcontenttype ("text/html");5 6String str = "Oh hehe";7OutputStream out =Response.getoutputstream ();8Out.write ("</br></br><div align=\" Center\ "style=\" font-size:25px; color:red\ ">". GetBytes ());9 Ten Out.write (Str.getbytes ()); One AOut.write ("</div>". GetBytes ()); - out.close (); -}
The byte stream setting is encoded as Utf-8 output:
1 Public voiddoget (httpservletrequest request, httpservletresponse response)2 throwsservletexception, IOException {3 4 //notify client to check UTF-8 code table5Response.setcontenttype ("Text/html;charset=utf-8");6 7 //or:8 //Response.setheader ("Content-type", "Text/html;charset=utf-8");9 TenString str = "Oh haha haha"; OneOutputStream out =Response.getoutputstream (); AOut.write ("</br></br><div align=\" Center\ "style=\" font-size:25px; color:red\ ">". GetBytes ()); - -Out.write (Str.getbytes ("Utf-8")); the -Out.write ("</div>". GetBytes ()); - out.close (); -}
BYTE stream output Number:
1 Public voiddoget (httpservletrequest request, httpservletresponse response)2 throwsservletexception, IOException {3Response.setheader ("Content-type", "Text/html;charset=utf-8");4 5 inti = 98;6OutputStream out =Response.getoutputstream ();7 8Out.write ("</br></br><div align=\" Center\ "style=\" font-size:25px; color:red\ ">"9 . GetBytes ());Ten One //Out.write (i); output letter B A - //Output Digital 98 -Out.write ((i + ""). GetBytes ()); the -Out.write ("</div>". GetBytes ()); - out.close (); -}
The character stream setting is encoded as Utf-8 output:
1 Public voiddoget (httpservletrequest request, httpservletresponse response)2 throwsservletexception, IOException {3 //notify client to check UTF-8 code table4Response.setcontenttype ("Text/html;charset=utf-8");5 //or:6 //Response.setheader ("Content-type", "Text/html;charset=utf-8");7 8String str = "Whoa, hey, hey.";9PrintWriter out =Response.getwriter ();TenOut.write ("</br></br><div align=\" Center\ "style=\" font-size:25px; color:red\ ">"); One A out.write (str); - -Out.write ("</div>"); the Out.flush (); - out.close (); -}
Chinese character set problem for servlet response