views:

334

answers:

3

I have an html-form with several text fields.

When I try to submit not English characters (Russian in my case) server is received "unreadable" string (not questions - "???" but some strange characters).

I simplified my code to show it here:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head><title>Simple jsp page</title></head>
  <body>
    <c:out value="${param.rustext}"/>
    <form action="/test" method="post">
        <input type="text" name="rustext" width="30">
        <input type="submit" value="Submit">
    </form>
  </body>
</html>

How should I fix that?

+4  A: 

Tomcat uses ISO-8859-1 a the default character decoder for URL parameters, regardless of the encoding of the page that contains the URL. This can be changed with the "URIEncoding" attribute in its Connector configuration. Other application servers may have similar settings.

This article covers many problems commonly encountered when working with JSP.

erickson
Yes, I use Tomcat. Thanks for the link, I'll try to find something usefull there.
Roman
See http://tomcat.apache.org/tomcat-5.5-doc/config/http.html for the specifics of the URIEncoding attribute.
erickson
This answer is *actually* wrong. The OP was using POST, not GET.
BalusC
A: 

When using POST - which is a must tu use encoding - the form is send as content-type "application/x-www-form-urlencoded". You may specify the form-attribute accept-charset="UTF-8" to specify your encoding.

Arne Burmeister
I've already tried and it doesn't work
Roman
+1  A: 

Erickson explained this very well on this page. A server-independent solution is to use a character encoding filter, à la org.springframework.web.filter.CharacterEncodingFilter. See example below:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
    private String encoding = "utf-8";
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain filterChain) throws IOException, ServletException {
        request.setCharacterEncoding(encoding);
        filterChain.doFilter(request, response);
    }
    public void init(FilterConfig filterConfig) throws ServletException {
        String encodingParam = filterConfig.getInitParameter("encoding");
        if (encodingParam != null) {
            encoding = encodingParam;
        }
    }
    public void destroy() {
        // nothing todo
    }
}

In web.xml add the filter declaration and the filter url mapping in the appropriate sections:

  <filter>
  <filter-name>EncodingFilter</filter-name>
  <description>
    <![CDATA[Changes the encoding of the request, in order to help the appserver to correctly interpret request params]]>
  </description>
  <filter-class>com.mypackage.EncodingFilter</filter-class>  
 <init-param>
    <param-name>encoding</param-name>
    <param-value>ISO-8859-15</param-value>
  </init-param>   
</filter>


  <filter-mapping>
      <filter-name>EncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
Victor Ionescu
Note that this only covers the request body, not the request URL. In other words: this will set encoding for POST requests, not for GET requests. For GET requests you still need to fiddle with the servletcontainer's config, as outlined in Erickson's answer.
BalusC
Right, I haven't thought about the URL.
Victor Ionescu