java - Servlet and jsp character encoding idiosyncrasy

23
2014-04
  • 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="com.xyz.foo.ConsoleApp" %>
    <html>
        <head>
            <meta charset="UTF-8"/>
        </head>
        <body><%= "Setúbal" %></body>
    </html>
    

    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 {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
    
        writer.println("<html>");
        writer.println("<head><meta charset='UTF-8'/></head>");
        writer.println("<body>Setúbal</body>");
        writer.println("</html>");
    }
    

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

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

    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
                 res.sendRedirect("/myApp/login.jsp");
             }
        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"/>
    </locale-charset-info>
    

    And In my jsp files I have this specified:

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    

    plus:

    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.