Reputation: 439
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?
Upvotes: 0
Views: 1062
Reputation: 1108692
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()
.
Upvotes: 2
Reputation: 5147
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/
Upvotes: 0