views:

314

answers:

2

I need to return some special Latin letter (e.g. á) in the response of a portlet serveResource() method. I have tried the following ways:

response.setCharacterEncoding("ISO-8859-1") ;
PrintWriter out = resWrapper.getWriter();
out.println("á");
out.close();

OR

response.setContentType("text/plain; charset=ISO-8859-1");
PrintWriter out = resWrapper.getWriter();
out.println("á");
out.close();

The front end XHR call (to the serveResource url) does not get the correct character back from either approach above. However, if the XHR posts the request to a HttpServlet (with the exact same response codes above), it works fine.

Can someone please shed some light on the problem here?

+1  A: 

Is it possible that the browser is interpreting the response from the serveResource() call according to the character set specified on the page containing the portlet instead of what you intend? Maybe you are getting the correct character back, but when the browser renders it, it disregards what you set in the serveResource() method and displays it with the same character set as the rest of the page.

That might explain why it works fine with an HttpServlet, which has responsibility for rendering the whole page, not just a piece of it. And so, setting the character encoding on the response seals the deal in that case.

cc1001
A: 

I would:

  • Save the resultant output to disk and do a hex dump; the value of U+00e1 (á) encoded as ISO-8859-1 should be E1. If this is so, there is something wrong with how the data is being interpreted on the client (check the HTTP headers). If this is not the case, there is a problem with how the data is being encoded (encoded as UTF-8, the character becomes the bytes C3 A1).
  • Try changing the output to out.println(\u00E1");. If this works, then the problem is in how the compiler loads and interprets the Java source. This is unlikely given that the servlet works.
McDowell