19

I have an API that returns XML, it actually returns it using the default encoding (I believe it's UTF-8), but now requirements have changed and we need to return everything in UTF-16LE.

My question is: is there an easy way of doing this? I have access to the response just before the calls complete so I was wondering if I could do something like

//This method does not exist
response.setCharacterEncoding("UTF-16LE");

Thanks a lot!

UPDATE: The method mentioned is the one to use. I was using an old version (2.3) of the servlet API that did not include it. Changing the version fixed it all.

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232

5 Answers5

20

Uhh, the method does exist, here

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it. 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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 2
    ..which is introduced in Servlet API 2.4. Although this is over 5 years old and nowadays everyone ought already have at least that version, you need to ensure that you're using it as well. – BalusC Dec 04 '09 at 19:21
  • 3
    It might be worth noting here that the default encoding is ISO-8859-1. – Rok Strniša Jan 07 '14 at 21:58
15

As others have stated, use either:

response.setCharacterEncoding("UTF-16LE");

or:

response.setHeader("Content-Type", "text/xml; charset=UTF-16LE");

...but make sure you do this before calling response.getWriter(); ...!

Tim Cooper
  • 10,023
  • 5
  • 61
  • 77
9

First

response.setHeader("Content-Type", "text/xml; charset=UTF-16LE");

Then, make sure you're actually emitting that encoding!

Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
1

I found that you MUST set the character encoding to at least UTF-8 because the default is ISO-8859-1. The ISO-8859-1 character set doesn't account for some extended characters. I wrote a helper function to use what is sent in the "Accept" header:

public static void setResponseCharacterSet(HttpServletRequest request, HttpServletResponse response)
{
    String type = "UTF-8";
    if(request.getHeader("accept") != null)
    {
        String[] params = request.getHeader("accept").split("charset=");
        if(params.length == 2) {
            type = params[1];
        }
    }
    response.setCharacterEncoding(type);
}
Derek Wade
  • 697
  • 8
  • 11
-2

just do the following thing:

byte[] k =xml.getBytes("UTF-16"); // xml is the string with unicode content.  getBytes("UTF-16") encodes given String into a sequence of bytes and returns an array of bytes. you can use xml.getBytes(UTF8_CHARSET); for utf-8 encoding

response.setContentType("text/xml");
response.setContentLength(k.length);
response.getOutputStream().write(k);
response.getOutputStream().flush();
response.getOutputStream().close();