Analysis and solution of PrintWriter return garbled

Source: Internet
Author: User

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

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.