2

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

Christian
  • 3,708
  • 3
  • 39
  • 60

3 Answers3

5

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" %>

Christian
  • 3,708
  • 3
  • 39
  • 60
1

Set the encoding of the ServletResponse:

response.setCharacterEncoding("UTF-8");
Moritz Both
  • 1,598
  • 1
  • 13
  • 21
  • 2
    This worked :) . But you probably lost points because of your brief answer (which i loved) . So happy! Was trying for hours! Thanks – Christian Nov 30 '17 at 13:05
  • @Christian so you're going to write the given line in **every** single piece of your code in the future, because this solved the problem? – Kayaman Nov 30 '17 at 13:06
  • I was going to put it in a BaseServlet that each Servlet extends – Christian Nov 30 '17 at 13:07
  • 1
    That's a very poor way to "solve" this problem. You don't need to explicitly set the character encoding in the response. That would be horribly tedious. – Kayaman Nov 30 '17 at 13:10
  • So your answer is to make sure every developer on every IDE on every Operating System ensures they save the file as UTF-8 for this to work? – Christian Nov 30 '17 at 13:11
  • @Christian that's how it is and why this answer is not really good. Also, what may happen if you change how you write web apps in Java i.e. working with a framework like JSF, Spring MVC or another alternative, do you have to figure out how to add this too on every response from the server side? – Luiggi Mendoza Nov 30 '17 at 13:12
  • Well I'd expect every developer to use UTF-8 unless they're looking for trouble. The thing is, you might implement this solution, thinking you've fixed everything. However you **will** run into problems with character encoding in the future, and they won't be fixed with this. You don't understand character encodings, and you don't even know if your source files are in UTF8 or not. [Here](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/) is some reading. You've got a config issue. – Kayaman Nov 30 '17 at 13:13
  • 1
    @LuiggiMendoza This is why I implement an abstract Servlet with settings like this defined which is then implemented from the concrete Servlet. – Christian Nov 30 '17 at 13:14
  • @Kayaman, Ive read that but I'm still waiting to read your answer. – Christian Nov 30 '17 at 13:15
  • @Christian and that will only work when you use pure Servlets, not when you do a more refined work. Also, you shall not change that response for some servlets e.g. when downloading a file. – Luiggi Mendoza Nov 30 '17 at 13:15
  • 1
    Folks, a) please calm down here (I saw an offensive comment which was deleted after seconds) and b) those who can propose a better solution may post another answer to the question, right? No need for advocacy. – Moritz Both Nov 30 '17 at 13:16
  • 1
    There are plenty of duplicates for this question. Go through those and see *all the parts* that need to be considered. Character encoding is a lot more complicated than people think, so putting out solutions like this gives the wrong impression about it. Manually setting the response encoding is not a solution, it's a hack. – Kayaman Nov 30 '17 at 13:19
0

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>
Laurent
  • 469
  • 3
  • 7