java - Servlet and jsp character encoding idiosyncrasy

  • Miguel Fonseca

    I have a webapp on which I need to display unicode characters. It's all fine when I write the strings in the jsp, for example:

    <%@ page contentType="text/html;charset=UTF-8" %>
    <%@ page import="" %>
            <meta charset="UTF-8"/>
        <body><%= "Setúbal" %></body>

    I get the desired output: Setúbal

    However, the equivalent code in a servlet does not render properly, for example:

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        PrintWriter writer = response.getWriter();
        writer.println("<head><meta charset='UTF-8'/></head>");

    The same thing happens when in the jsp I load the text from a class:

    <%@ page contentType="text/html;charset=UTF-8" %>
    <%@ page import="" %>
            <meta charset="UTF-8"/>
        <body><%= ConsoleApp.getText() %></body>

    In both cases I get strange characters: Set√∫bal

    All files are UTF-8 and the response headers have the following:

    Content-Type    text/html; charset=utf-8
    Content-Encoding    gzip
    Date    Tue, 09 Nov 2010 09:44:05 GMT
    Server  Google Frontend
    Cache-Control   private, x-gzip-ok=""
    Content-Length  438
  • Answers
  • Christoffer Hammarström

    Use the javac -encoding parameter to tell javac what encoding your java source is stored in, otherwise it uses the platform default which apparently isn't UTF-8.

  • Related Question

    java - JSP UTF-8 char encoding error on Glassfish 3.01
  • chickenloop

    I have a rather starange problem. In my web application I use UTF_8 encoding. This works fine until I put a jsp page into my /Protected/ folder. The exact same jsp file works just fine uder my document root. To reach the /protected/ folder I use a Filter. Like this:

        public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
    throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        PrintWriter out = res.getWriter();
        if (session.getAttribute("user") == null){
            // redirect the user to the login page and give error message
        else {
        chain.doFilter(request, response);

    In my sun-web.xml I have this specified:

    <locale-charset-info default-locale="">
    <parameter-encoding default-charset="UTF-8"/>

    And In my jsp files I have this specified:

    <%@taglib prefix="c" uri=""%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>


    Any help is much appreciated! Thanks in advance!

  • Related Answers
  • BalusC

    Get rid of the following line in the filter.

    PrintWriter out = res.getWriter();

    You only don't need it in a filter at all, but also when the outputstream or writer of the response is been created for the first time, you cannot alter its character encoding afterwards anymore.