Wednesday, January 16, 2013

HttpServeltResponse send error message in UTF-8

I used to fix a bug of httpServletResponse sending error message in utf-8. It was the response of an ajax call. If there was error, it need send error with error message back to the client to trigger the method below in client javascript.

error: function( jqXHR xhr, String textStatus, String errorThrown ) 


The previous guy used this code in java



response.sendError(11, errorMessage);


and tried to get the error message with this code in javascript.



xhr.statusText


This did not work if there was French character with accent in message.



The solution is like this, in java code



response.setStatus(11);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(errorMessage);


in javascript, get responseText instead of statusText



xhr.responseText


Before I found this, I also tried this. Although it worked on UI, but I saw error messages in log file from weblogic.



response.setContentType("text/html;charset=UTF-8");
response.sendError(11, errorMessage);


The error message is about the



ProtocolException: Didn't meet stated Content-Length


Here is a similar issue

No comments:

Post a Comment