0

I am running Spring Framework on GlassFish 4.0 and developing in NetBeans.

I have a web page with UTF-8 content:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

And just to be sure the form has also been set to UTF-8.

        <form:form commandName="DocumentsCmd" acceptCharset="UTF-8">

I have an entry in a select control containing Slovenian characters:

<option value="PC plačilne liste za zadnje 3 mesece">PC plačilne liste za zadnje 3 mesece</option>

This displays perfectly in the browser, however when I POST the form the String in Java comes through as:

PC plaÄilne liste za zadnje 3 mesece

So it seems that Glassfish and/or Spring are not interpreting the form as UTF-8.

After doing some research I've tried adding:

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

<filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

To web.xml, however that has made no difference.

What have I missed that will cause Spring/GlassFish to interpret the incoming POST request as UTF-8?

I just found this: CharacterEncodingFilter don't work together with Spring Security 3.2.0 which may be relevant as I do have this at the end of my web.xml:

<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

That question and answer are talking about Tomcat not GlassFish though...

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128

1 Answers1

0

I just resolved this by adding a glassfish-web.xml to the project and adding these lines within it:

<sun-web-app>
    <parameter-encoding default-charset="UTF-8" />
</sun-web-app>
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • Did you add the whole snippet you've posted above (i mean, inside glassfish-web.xml? Or just the parameter-encoding line. I'm facing the same problem and tried adding the default-charset into the glassfish-web.xml but it didn't work. And it seems to me this is valid only on a sun-web.xml – Endrik Sep 17 '14 at 08:45
  • @Endrik I added the whole snippet including the `sun-web-app` tags. This was inside `glassfish-web.xml`. It parsed fine and fixed the problem. – Tim B Sep 17 '14 at 09:21
  • Then i guess it should depend on my custom filter...Thank you for replying @Tim B :) – Endrik Sep 17 '14 at 09:22