2

We are on a production server which uses Tomcat 7, and for whatever reason, the administrators refrain from setting the CATALINA_OPTS="-Dfile.encoding=UTF-8" workaround for us.

This means that now when deploying the WAR file, Tomcat returns garbled characters instead of their Unicode counterparts.

We have also included this in our web.xml, to no avail:

<filter>
    <filter-name>encoder</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>encoder</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

How do we manage this without sending a man to breach into the server room and modify the configuration?

Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
  • if autodeploy is enabled just put updated war into webapp dir and it will work. if not you need request tomcat restart. – JIV Oct 01 '12 at 14:20
  • My problem is not access to Tomcat or having it restarted. It is just that I am unable to set an environment variable for Tomcat. – Milad Naseri Oct 01 '12 at 14:21
  • Can you get your server admin to change the OS default encoding to UTF-8? – dngfng Oct 01 '12 at 14:23
  • Can you change tomcat's config files? – Martin Wilson Oct 01 '12 at 14:28
  • @dngfng No. It is "vital to not modify operating system configuration", according to the admin. – Milad Naseri Oct 01 '12 at 14:30
  • @Martin Wilson: Yes, we can actually do that. – Milad Naseri Oct 01 '12 at 14:31
  • "Tomcat returns garbled characters instead of their Unicode counterparts" - do you mean in your site/apps pages when viewed in a browser? If so, is this for a) static data in your resources, e.g. JSP files, b) data from the database c) data entered by users d) anything else? – Martin Wilson Oct 01 '12 at 14:48
  • If it's data from properties files (as per your comment to BalusC), did you see this? http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle The filter above will only affect POST data – Martin Wilson Oct 01 '12 at 15:02
  • @MiladNaseri Where that `garbled characters` was originally placed? .jsp, .properties, .java files, something else? – user1516873 Oct 01 '12 at 15:13

1 Answers1

2

First of all, it's important to understand what the -Dfile.encoding=UTF-8 exactly does. It is a Sun/Oracle JVM-specific setting (which thus don't necessarily work in all other JVMs!) which basically instructs the JVM to read the Java .class files using the given encoding instead of the platform default one. So, setting this would only solve any possible Mojibake problems which is caused by using "special characters" in Java class/variable names or hardcoded String values in Java classes (yes, you read it right: only Java classes, not other files, thus definitely not properties files or JSF XHTML files, etc).

In all honesty, I can hardly imagine that this is the right solution to your concrete problem. Why would one ever use special characters straight in Java classes? Class/variable names should be in all English and localized text should be placed in resource bundle files. Every self respected Java developer adeheres this convention.

Given that fact, and assuming that you are also not using special characters in Java classes at all, I thus believe that your concrete problem is caused by something else. The problem symptoms are not specific enough described (at which step exactly does it fail? which characters exactly do you expect and get instead? etc) to see the possible root cause of your concrete problem. I can at least tell that the URL pattern of your Spring filter is completely wrong. It must be mapped on /*

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

(by the way, you don't necessarily need Spring for this, just a custom Filter with only 2 or 3 lines in doFilter() implementation is already sufficient)

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Of course, I am only storing such values inside *.properties files in the classpath (under `/resources/`). I don't use Spring for only this, but here I have used it to not have to rewrite this fairly simple code. I use Spring as my IoC container. – Milad Naseri Oct 01 '12 at 14:37
  • Thanks for the tip. I changed the url-pattern, as suggested, and nothing changed. – Milad Naseri Oct 01 '12 at 14:44
  • Then describe the problem symptoms in more detail. "It returns garbled characters" is absolutely insufficient information. Try describing the problem in developer's perspective, not in enduser's perspective. Reading the "See also" article may help in better understanding how it works under the covers and using the right terminology. It by the way also describes the solutions in detail. – BalusC Oct 01 '12 at 14:47
  • In the end, I got around this by manually converting the text read from ".properties" files which were in UTF-8 format and handing them over to the web application. It was possible due to your "see also", which by the way, was quite awesome. Thanks for the answer, and sorry for the late accept. I was rather busy. – Milad Naseri Oct 13 '12 at 12:13