2

I've been developing a web application using spring mvc. When I deploy it on openshift using tomcat 7 and mysql database I get the following issue. Greek letters coming from an HTML MULTIPART form are saved in the database like:

ÃÂÃÂÃÂÃÂýñÃÂ

The issue doesn't appear in my local deployment where the characters are correctly saved as Greek letters. In order to ensure UTF-8 encoding in my jsp and html i used:

<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

In my web.xml:

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

In order to connect to the DB I use the following parameters in the connection URL: ?useUnicode=true&characterEncoding=UTF-8

I've also changed the following DB parameters to utf-8: character_set_client, character_set_connection, character_set_database, character_set_results, character_set_server and the collations to utf8_general_ci. This was done through SET queries.

Any ideas on what may cause the encoding issue and how to solve it?

UPDATE: HTML form is multipart

kostas
  • 51
  • 8
  • After some experimentation it seems that the database supports Greek characters but somehow the encoding is messed up after the form submission. Still unsure on what's wrong. Moved the app to Cloudbees and everything seems to work fine out of the box! – kostas Aug 13 '14 at 13:45

1 Answers1

2

The issue has to do with the following tomcat bug for version 7.0.40 (openshift default at the time of question):

https://issues.apache.org/bugzilla/show_bug.cgi?id=54984

In short, tomcat messes up the encoding of multipart form data. Workaround:

You can use the filter hereafter instead of the default multipart filter or update tomcat to version 7.0.41 or higher. Please note that this solution has not been tested on openshift.

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.support.MultipartFilter;

public class MyMultipartFilter extends MultipartFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request,
       HttpServletResponse response, FilterChain filterChain)
       throws ServletException, IOException {

       request.setCharacterEncoding("UTF-8");
       request.getParameterNames();

       super.doFilterInternal(request, response, filterChain);
    }
}
kostas
  • 51
  • 8