File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpServletResponse character encoding - best practice?" Watch "HttpServletResponse character encoding - best practice?" New topic

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:


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?

Monika Joshi

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.

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:
subject: HttpServletResponse character encoding - best practice?
It's not a secret anymore!