protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, I oexception {PrintWriter pw = Response.getwriter (); Response.setcharacterencoding ("Utf-8"); Response.setcontenttype ("text/html; Charset=utf-8 "); Pw.print ("Chinese");}
protected void doget ( httpservletrequest request, Httpservletresponse response) throws ServletException,IOException { printwriter pw = response.getwriter (); response.setcharacterencoding (" Utf-8 "); Response.setcontenttype ("Text/html; charset=utf-8"); pw.print ("Chinese"); }
The output is garbled. Why, has the character encoding been set? Isn't the setting invalid.
Find the description of the method in the API:
public void setcharacterencoding (String charset) { if (iscommitted ()) return; // Ignore any call from an included servlet if (included) return; // ignore any call made after the getwriter has been invoked // the default should be used if (Usingwriter) return; coyoteresponse.setcharacterencoding (CharSet); ischaracterencodingset = true; }
Public void setcharacterencoding (String charset) { if (iscommitted ()) return; // ignore any call from an included servlet if (included) return; // ignore any call made after the getWriter has been invoked // the defaUlt should be used if (Usingwriter) return; Coyoteresponse.setcharacterencoding (CharSet); ischaracterencodingset = true; }
where the Usingwriter flag is set in the Getprintewriter method, it can be seen that the control logic is no longer valid once the PrintWriter is returned.
Servletoutputstream out = Response.getoutputstream ();
Out.print ("Chinese");
Case 1: normal, the browser utf-8 view
//response.setcontenttype ("text/html; Charset=utf-8 ");
Case 2: Browser default view by Simplified Chinese Manually set the Utf-8 method to view the normal
//response.setcharacterencoding ("Utf-8");
Note: This method does not need to set the character set before calling Getoutputstream (), even after the print output is set to be valid.
Conclusion:
1. In the servlet output Chinese, if the PrintWriter mode, you need to call Getprintwriter () before calling setContentType or setcharacterencoding The Servletoutputstream method is not subject to this limitation.
The method of setting characterencoding in the 2.setContentType and setcharacterencoding two methods is consistent with the server effect and does not need to be called repeatedly. When outputting text content, use Response.setcontenttype ("text/html; Charset=utf-8 "); it seems more convenient.
3.PrintWriter itself does not handle the responsibility of coding, it should be regarded as an adorner is better: it is for the output more convenient and designed to provide print, println, printf and other convenient methods. To set the encoding, it can be set on its underlying writer: (here OutputStreamWriter is the underlying writer), refer to:
New PrintWriter (New OutputStreamWriter (New FileOutputStream ("Yourfilepath"), "UTF-8");
Analysis and solution of PrintWriter return garbled