2
votes

I am setting the response content type to : "text/event-stream; charset=UTF-8" and response character encoding to "UTF-8" in the servlet.

However, response.getCharacterEncoding() prints "ISO-8859-1" in the terminal.

JAVA Code:

response.setContentType("text/event-stream; charset=UTF-8");
response.setCharacterEncoding("UTF-8");

System.out.println(response.getCharacterEncoding());

It seems that character encoding is not set properly. How can i resolve this? I have lost many hours figuring reason of this issue. Please help.

1
setContentType Sets the content type of the response being sent to the client, if the response has not been committed yet. Can you make sure that nothing has been written in the response when you call the method?Guy Bouallet
@GuyBouallet I was just getting a PrintWriter from response before these lines. Even though i was no writing anything, that was causing the issue. I have moved that code after these lines and now everything works fine. Can you put your reply as an answer. I will accept it. Thanks for help.Gautam Kumar

1 Answers

0
votes

setContentType Sets the content type of the response being sent to the client, if the response has not been committed yet. You have to make sure that nothing has been done with the response before you call the method.