The use and difference of outputstream and PrintWriter

Source: Internet
Author: User
1.OutputStream

Use steps: Get output stream set Chinese convert a string to a byte array call Outputstream.write ()

Here only the contents of the Doget method are posted:

    protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException c2/>//gets OutputStream output stream
        outputstream outputstream = Response.getoutputstream ();
        Set Chinese
         response.setheader ("Content-type", "Text/html;charset=utf-8");
         String data = "Hello, here is the OutputStream way to the page output content";
        Converts a character to an array of bytes and specifies the UTF-8 encoding
         byte[] Databytearr = data.getbytes ("UTF-8");  output
         outputstream.write (Databytearr);
    }

2.PrintWriter

Using steps: Set document type and encoding to get getwriter () output

is also the content that only pastes the Doget method:

Set the response content type and encoding mode
response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Out.println ("Hello, here is the PrintWriter way to the page output");

3. Summary

The outputstream is output in bytes, and the PrintWriter is output in character form.
The difference between a byte stream and a character

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.