5

I have a web app with spring,jsp and jquery in a apache tomcat 6, one jsp page has a form that send the data with a ajax call made whit jquery, to a Spring MultiActionController on my back end.

The problem is with the UTF-8 strings in the form inputs.

I already did the following things:

On my HTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page language="java" pageEncoding="utf-8"%>

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

On the jquery ajax call:

$.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8" });
    $.ajax(
        {
            type: "GET",
            url: "./saveData.action",
            contentType: "charset=utf-8",
            data: { name: $('#name').val(),...

On the tomcat server.xml:

<Connector connectionTimeout="20000" port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" redirectPort="8443"/>

On the MultiActionController

public ModelAndView saveData(HttpServletRequest request,HttpServletResponse response) 
    throws ServletException, IOException 
    {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");

So if in the name parameter I put something like this:

María

in the backend I get

María.

I already tried all the things that I read about, and I don't know what is the error, thanks for any help :)

2 Answers 2

16

Everything looks fine until the point that you get the parameter in a variable. It's just your backend which still needs to be configured to use UTF-8. For example, the System.out.println() or the logger where you're sending the retrieved parameter to should also use UTF-8. Or the database where you're storing the retrieved parameter should also use UTF-8. Or the JDBC driver which is interacting with that DB. Or the text file where you're writing the data to. Etcetera.

See also:


Unrelated to your concrete problem, the line

request.setCharacterEncoding("UTF-8");

should be executed before Spring kicks in. It sets the POST request encoding and this will fail whenever Spring determines the request body before executing the action (note that this line has totally no effect on GET requests). Spring has a CharacterEncodingFilter which does exactly that. Register this in your 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>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks BalusC, I changed the DB and also de web.xml and now it works fine :)
how to add this to web.xml? inside <web-app>...</web-app> tags? because when I did so my project doesnt run succesfully.
I was having this exact same problem, adding the filter to the xml did the trick. I'm not sure if I have utf-8 installed on my dev server but I am manually converting to unicode.
2

I had the same problem from with Characters and I solved it the following way:

request.setCharacterEncoding("utf-8");
                StringBuffer requestContent = new StringBuffer();
                do
                {
                    bytesRead = request.getInputStream().readLine(bytes,0,bytes.length);
                    if(bytesRead > 0)
                    {
                        requestContent.append(new String(bytes,0,bytesRead,"UTF-8"));
                    }
                }
                while(bytesRead > 0);

and then fetch from requestContent your string started with "name="

1 Comment

The request.setCharacterEncoding() has totally no effect on GET requests. The request.getInputStream() returns nothing on GET requests. It's the URIEncoding in Tomcat's <Connector> which counts for GET requests. That's already been set properly. Still then, your proposed approach is a workaround, not a solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.