0

I have a java web project running Servlet 2.4 on Apache Tomcat.

In my servlet I have set request.setCharacterEncoding("utf-8") and using the <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> in HTML head tag.

All files (Java, JS etc.) in the project have text file encoding set to utf-8. I have also added a Filter mapped to all Servlets in web.xml which sets character encoding to utf-8.

When making ajax requests (both get and post) to the web server where I use jQuery, and the serialize method on a html form, the Servlet is unable to retrieve special utf-8 characters.

Maybe it's because it expects UTF-8 and gets URLencoded string? Has anyone any tips regarding this?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
sjallamander
  • 439
  • 2
  • 6
  • 20

2 Answers2

2

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

Meta http-equiv tags in the HTML head are ignored when the HTML page itself is been served by a true HTTP request. Instead, the information provided in HTTP response headers are been used. The meta http-equiv tags are only used when the page is not been obtained as a HTTP resource via http:// URI, such as the local disk file system via file:// URI, which may happen when the enduser saved the obtained HTML file to disk file system and reopened it from there via file explorer.

You should now probably realize why the attribute is called exactly like that: http-equiv, as in "HTTP equivalent".

So, you need to set the content type and character encoding in the real HTTP response header. This can be done by placing the following line in top of JSP:

<%@page pageEncoding="UTF-8"%>

Or, if you intend to apply this on all JSPs of your webapp in a single place instead of copypasting the same over all files, then put the following in webapp's web.xml:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

Please note that I assume that you're sending jQuery ajax requests the right way and that you're properly URI-encoding the parameters using e.g. $.serialize() or encodeURIComponent().

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Don't use JSP to produce HTML output. Using (or abusing if you want) PrintWriter in Servlet to print html code.. The weird thing is that it works when I specify special UTF-8 characters in parameters with the data object in $.ajax, but not with the $.serialize() function. – sjallamander Sep 26 '13 at 18:26
  • More precisely: don't use JSP to produce on ajax responses. Yes, agree on this part. See also e.g. http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax/4113258#4113258. As to the concrete problem, apparently the physical JSP file itself isn't saved using UTF-8. – BalusC Sep 26 '13 at 18:33
0

Use this contentType when execute jQuery.ajax:

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

More details here: http://api.jquery.com/jQuery.ajax/

Archer
  • 5,073
  • 8
  • 50
  • 96