0

I home some problems with encoding of data in JSP page (I'm using a Spring-MVC). It looks like this:

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

<%
     response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
     response.setHeader("Pragma", "no-cache"); //HTTP 1.0
     response.setDateHeader("Expires", 0);
     response.setCharacterEncoding("UTF-8");
     request.setCharacterEncoding("UTF-8");
%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

.....
<form method="post">            
        <input type="text" name="txt" value="${Name}" />
        <input type="submit" value= OK /> 
</form>

........

My app takes a text from input text box and write it to the database (HSQLDB). When I'm using english letters to write data, it all works fine! But when I'm trying to use russian letters in input text box, it write some strange letters in incorrect encoding form. How to avoid this problem?

Any ideas?

Ilnur
  • 646
  • 1
  • 9
  • 18
  • 1
    Your `Cache-Control` entry is by the way [incomplete](http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers/2068407#2068407). It won't work in Firefox, among others. Also, the both `setCharacterEncoding()` calls are superfluous since it's too late for the `request` one and you already have set the `pageEncoding` which already implicitly does the `response` one. – BalusC Nov 28 '10 at 18:32
  • Oh... I didn't know about it. So, I can remove lines response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); ? I tried to put this line here to solve encoding problems. But all didn't worked, until axtavt don't gives me a solution with Spring encoding filter. – Ilnur Nov 28 '10 at 18:55
  • 1
    As said, it's *too late* to set it on the request. It's already been processed (JSP is part of the response). It need to happen *before* request processing and a `Filter` is the only right place to do the job. For the response, it's just duplication since the `pageEncoding="UTF-8"` already does exactly that under the covers. By the way, setting the response headers should preferably also happen in a filter. This keeps your JSPs free of code duplication clutter. See also [how to avoid Java code in JSP?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files). – BalusC Nov 28 '10 at 18:58

1 Answers1

5

You need to configure character encoding filter in web.xml:

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

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

Servlet container treats POST request data as ISO-8859-1 if request encoding is not specifed, whereas browser submits the form in the page encoding (UTF-8 in your case) and unsually doesn't specify the encoding, therefore you need a filter to specify the proper request encoding.

EDIT: As correctly mentioned by BalusC, it doesn't cover encoding problems with GET requests. To fix that on Tomcat you need to set URIEncoding="UTF-8" on <Connector> in conf/server.xml.

axtavt
  • 228,184
  • 37
  • 489
  • 472
  • 2
    Good advice for Spring users, but noted should be that this only covers POST requests, not GET requests. It's by the way a [simple filter](http://kickjava.com/src/org/springframework/web/filter/CharacterEncodingFilter.java.htm). All it basically does is `request.setCharacterEncoding()` if `request.getCharacterEncoding()` returns null. – BalusC Nov 28 '10 at 18:34
  • Great!!! Thanks a lot, axtavt! I tried to solve it by configuring Tomcat settings, but all doesn't help me. – Ilnur Nov 28 '10 at 18:35
  • 1
    Any setting which Tomcat offers for request encoding concerns GET requests, not POST requests (the `URIEncoding` attribute of HTTP `` element). – BalusC Nov 28 '10 at 18:37