aspose file tools*
The moose likes Servlets and the fly likes HttpServletResponse character encoding - best practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpServletResponse character encoding - best practice?" Watch "HttpServletResponse character encoding - best practice?" New topic
Author

HttpServletResponse character encoding - best practice?

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
I use the Netbeans IDE, and its servlet template uses this line:

response.setContentType("text/html;charset=UTF-8");

Is that best practise though for setting the response's character encoding? I was wondering if it would be better to either:
1. Use the same character encoding for the response as was detected for the request
or 2. Examine the "accept-charset" header of the request to detect and use the client's preferred character encoding for the response.

Does anyone know what the best practice would be?

Thanks,
James
Monika Joshi
Greenhorn

Joined: Apr 19, 2010
Posts: 9
Hi James, Here the MIME type of the following document is specified.Default for servlets is text/plain, but they usually explicitly specify text/html.For eg: if the document type were image, audio, video and application, the content type that dhd be set would be image/jpeg, audio/mp3, video/mp4, and application/msword respectively.

Regards,
Minakshi
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Thanks for the reply Minakshi, but I was really asking about the character encoding rather than the MIME types. I.e., if the client sends you its data with ISO-8859-1 encoding or says it prefers that encoding, would it be considered best practice to return text content with the same encoding or just stick to UTF-8?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpServletResponse character encoding - best practice?
 
Similar Threads
CharacterEncoding
How does PrintWriter really work (with ServletOutputStream)?
How to force encoding of web browser to UTF-8
character encoding problem
charsets didn't get it