Difference between the getinputstream method and getreader method of httpservletrequest

Source: Internet
Author: User

 

Through the previous study, I learned that httpservletrequest is used to accept data transmitted by the client. Today I learned from instructor Zhang about the getinputstream and getreader methods of httpservletrequest and their differences, according to different situations, I chose to use them. I learned the importance of character set encoding in instructor Zhang's basic java video. This time, getwriter () is also used for redirection () the effect of the method on the character set encoding of the webpage is summarized below.

Getinputstream and getreader methods of httpservletrequest
1. The getinputstream method returns an input stream object representing the object content. The type is javax. servlet. servletinputstream, which can read the object content in the HTTP request message. If the object content contains binary data, you can only use the input stream object returned by this method to read.
The 2getreader method is used to return a bufferreader object that represents the object content. The returned bufferreader object encodes the byte data in the object content into a text string according to the character set specified in the request message.
3. When the entity content in the request message is all text dataProgramThe getreader method is also applicable when you need to process the content in text mode. If the servlet program does not process them, but is only stored in the file system or database of the server, this is suitable for use.

Getinputstream method.
4. For servlet programs that want to process file uploads, only the servletinputstream byte stream object returned by the getinputstream method can be called to read the object content, rather than the getreader method. Because the data read from the servletinputstream Stream object is the original content of each byte, and the data read from the bufferedreader Stream object is the original content converted to character text according to some encoding.

effect of the getwriter () method on Webpage character set encoding during redirection
1. the caller program does not use printwriter out = response. getwriter (); statement. The character set encoding set in the caller program overwrites the character set encoding set in the caller program.
2. The caller program uses the printwriter out = response. getwriter (); statement, and the character set encoding set in the caller program will no longer work. Because the printwriter object takes effect only when the getwriter method of the current httpservletresponse object is called for the methods that set character set encoding (that is, the setcontenttype () method used by the caller, the call after the getwriter method is invalid for the printwriter object

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.