Problems with encoding UTF-8, JSP, jQuery, Spring

I have a web application with spring, jsp and jquery in apache tomcat 6, one jsp page has a form that sends data using an ajax call made with jquery to Spring MultiActionController at my end.

The problem is UTF-8 lines in form inputs.

I have already done the following things:

In 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> 
.
.

In 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 tomcat server.xml:

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

In 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:

Maria

in the backend I get

Maar.

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

+5
source share
2 answers

, . , UTF-8. , System.out.println() , , UTF-8. , , UTF-8. JDBC, . , . Etcetera.

. :


,

request.setCharacterEncoding("UTF-8");

, Spring. POST, , Spring ( , GET). Spring CharacterEncodingFilter, . 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>
+16

, :

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);

requestContent , "name="

+2

All Articles