How to return the content in the correct encoding from a servlet?
The doGet() method of a servlet usually returns content to the requester. The most common way to do so is to get a java.io.PrintWriter from the HttpServletResponse, then use the write() method to write the content of the PrintWriter.
Example I
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
…
String xml = "some xml";
resp.setContentType("text/xml");
PrintWriter out = resp.getWriter();
out.write(xml);
}
When the content contains some non-ascii characters, you need to make sure the they are in the correct encoding, otherwise the requester will get non displayable characters. There are two ways to do it. The first one is to call the setCharacterEncoding(encodingOfYourChoice) of the HttpServletResponse.
Example II
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
…
String xml = "some xml";
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/xml");
PrintWriter out = resp.getWriter();
out.write(xml);
}
The other way is to add the charset property in the content type:
Example III
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
…
String xml = "some xml";
resp.setContentType("text/html; charset=UTF-8");
PrintWriter out = resp.getWriter();
out.write(xml);
}
Note, both calls in example II and III need to be called before the getWriter() method. Once the getWriter() method is called, the charset can't be changed. When setting the contentType without the charset property, the OS's default charset will be used, such as ISO-8859-4.
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- What are class variables in Java?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- What are local variables in Java?
- How to Use Updatable ResultSet in JDBC?
- How to Use JDBC Java to Create Table?
- Why final variable in Enhanced for Loop does not act final?