Christian
Christian

Reputation: 3972

Displaying UTF-8 text in a JSP page from a Servlet

When I try to display German text e.g., Zurücksetzen on a JSP through request.setAttribute(), it comes out as Zur�cksetzen.

request.setAttribute("test", "Zurücksetzen");

My JSP page defines contentType as UTF-8:

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

and I am displaying the attribute simply with ${test}.


The text is displayed correctly if I forward the request to the JSP page instead of include the JSP

Forward (working):
request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request, response);

Include (not working):
request.getRequestDispatcher("/WEB-INF/views/index.jsp").include(request, response);


My IDE is using UTF-8

enter image description here

Upvotes: 3

Views: 2326

Answers (3)

Laurent
Laurent

Reputation: 519

As explained here with Spring, the best way is to set the response to UTF-8 globally inside the web.xml using the Spring filter CharacterEncodingFilter :

<filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
       <param-name>encoding</param-name>  
       <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
       <param-name>forceEncoding</param-name>  
       <param-value>true</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>

Upvotes: 0

Christian
Christian

Reputation: 3972

Answering my own question:

As set out in JSP Globalization Support, the defaults are as follows

The default MIME type is text/html for traditional JSP pages; it is text/xml for JSP XML documents.

The default for the page source character encoding (for translation) is ISO-8859-1 (also known as Latin-1) for traditional JSP pages; it is UTF-8 or UTF-16 for JSP XML documents.

The default for the response character encoding is ISO-8859-1 for traditional JSP pages; it is UTF-8 or UTF-16 for JSP XML documents.

The determination of UTF-8 versus UTF-16 is according to "Autodetection of Character Encodings" in the XML specification, at the following location

So the Servlet and JSP pages are by default ISO-8859-1.

request.getRequestDispatcher("/WEB-INF/views/index.jsp").include(request, response);
When you .include a JSP page, as above, the page is encoded using the default character encoding (ISO-8859-1). In order to use UTF-8 encoding, you have to set response.setCharacterEncoding("UTF-8"); Note: the ContentType directive in the JSP is ignored.

request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request, response);
When you .forward to a JSP page, as above, or directly access a JSP page from the browser, the page is encoded using the default character encoding (ISO-8859-1). In order to use UTF-8 encoding, you have to add as the first line of the JSP page <%@ page contentType="text/html; charset=UTF-8" %>

Upvotes: 5

Moritz Both
Moritz Both

Reputation: 1628

Set the encoding of the ServletResponse:

response.setCharacterEncoding("UTF-8");

Upvotes: 1

Related Questions