Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added alternative method of handling UTF-8 problem.

...

(from the tomcat-user mailing list)

Alternative solution

The solution suggested above works fine with steps (1) and (2) only, but from the architecture perspective the correct way is to add a filter to the Tomcat that will do necessary correction for the application deployed without any additional changes to the rest of the code.

Example of filter:

{{{import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;

public class CharsetFilter implements Filter
{
private String encoding;

public void init(FilterConfig config) throws ServletException
{
encoding = config.getInitParameter("requestEncoding");

if( encoding==null ) encoding="UTF-8";
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
throws IOException, ServletException
{
request.setCharacterEncoding(encoding);
next.doFilter(request, response);
}

public void destroy(){}
}
}}}

Corresponding portion of web.xml configuration will look like:

No Format
  <!--CharsetFilter start-->

  <filter>
    <filter-name>Charset Filter</filter-name>
    <filter-class>CharsetFilter</filter-class>
      <init-param>
        <param-name>requestEncoding</param-name>
        <param-value>UTF-8</param-value>
      </init-param>
  </filter>

  <filter-mapping>
    <filter-name>Charset Filter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--CharsetFilter end-->

Wiki Markup
The suggested solution originates from \[http://people.comita.spb.ru/users/sergeya/java/ruschars.html Sergey Astakhov (all texts are in russian)\] (sergeya@comita.spb.ru)