简体   繁体   中英

OutputStreamWriter encoding vs response content-type

I have an OutputStreamWriter in my Servlet that uses a particular encoding scheme, ie I have to use this constructor

OutputStreamWriter(OutputStream out, String charsetName)

Also, I have used the following line of code to set the encoding scheme of the response

response.setContentType("text/html;charset=UTF-8")

Using this output stream I am sending response to the client.

Now in the browser the decoding will be done by which scheme UTF-8 or charsetName. Can someone explain me why?

The line

OutputStreamWriter(OutputStream out, String charsetName)

tells the writer which charset to use for encoding.

The line

 response.setContentType(text/html;charset=UTF-8)

sets the contentType header in the http response and tells the browser which encoding to use for displaying the content.

The browser will handle the content based on the Content-Type header. The charset you use for the OutputStreamWriter only affects how characters written to it are encoded into bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM