0

In my application are a bunch of message bundles as properties files. The messages are looked up in JSP files via <spring:message code="code.here">. This works and all special characters are rendered as they should in the browser.

Now I needed to get some extra logic into one of the JSP files and added a servlet in its place that does its thing and renders the JSP with req.getRequestDispatcher("/jsp/some_file.jsp").include(req, resp);.

This mostly works but now all special characters (German umlauts in my case) are broken. For some reason the umlauts that come out of the message tag are rendered incorrectly. By way of example the word "ungültig" comes out as

00000000  75 6e 67 ef bf bd 6c 74  69 67 0a                 |ung...ltig.|

in the browser. I know that this is a UTF-8 encoded Unicode replacement character but what I don't know is why it's there.

The resource files all have the correct encoding, in fact, to play it safe, our resource files are all ASCII and "ungültig" is in fact encoded as ung\u00FCltig, where 0xFC is the encoding of ü in ISO-8859-1 which is by definition the required encoding for Java's properties files.

I have no idea why this happens. Why is it suddenly rendering incorrectly and how do I fix it?

musiKk
  • 14,751
  • 4
  • 55
  • 82

1 Answers1

0

The solution is that for some reason unbeknownst to me, the directives regarding encoding in the JSP's header are ignored when using the request dispatcher.

There are two options for fixing this:

  1. Explicitly set the encoding in the servlet with resp.setCharacterEncoding("UTF-8").
  2. Set the value of the parameter forceEncoding of Spring's CharacterEncodingFilter to true, e.g.

    <filter>
        <filter-name>encoding-filter</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>
    

I used the second approach. I'm not sure whether this has any unintended side effects but for now it seems to work just fine.

musiKk
  • 14,751
  • 4
  • 55
  • 82