1

I have learned that in general, Java uses UTF-16 as the internal String representation.
My question is what actually happens when composing a response in Java and applying different char encoding, e.g. response.setCharacterEncoding("ISO-8859-1").

Does it actually convert the response's body bytes from UTF-16 to ISO-8859-1 or it just adds some metadata to the response object?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
itamar
  • 1,800
  • 4
  • 17
  • 30
  • 1
    It would help if you could say exactly which class you're talking about. – Jon Skeet Jul 31 '16 at 08:35
  • @BalusC could you please explain why its not Java questoin? – itamar Jul 31 '16 at 09:26
  • 1
    This is a Servlet API question not a Java SE API question. Use [java] tag only for questions demonstrable with a plain Java application class with `main()` method and/or answerable with JLS. – BalusC Jul 31 '16 at 09:32

1 Answers1

0

I'm assuming you're talking about a class that works along the lines of HttpServletResponse. If that's the case, then yes, it changes the body of the response, if you call getWriter. The writer that is returned by that has to convert any strings that are written to it into bytes, and the encoding is used for that.

If you've set the content type, then setting the content encoding will also make that information available via the Content-Type header. As per the ServletResponse docs:

Calling setContentType(java.lang.String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194