Reputation: 1817
I have a form in which the user enters his name in Chinese, but when I do
String strName = request.getParameter("name");
I get strName as some meaningless characters. As a solution I tried
request.setCharacterEncoding("UTF-8");
before reading any parameters from the request object. This worked. What I want to know is how do I achieve this in HTML/javascript . I have tried the
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
but this doesn't work . Any help?
Upvotes: 10
Views: 21380
Reputation: 5022
In pure HTML you should need <form enctype="multipart/form-data" accept-charset="UTF-8">
set to submit utf-8 in the browser.
Upvotes: 0
Reputation: 201538
What you do client-side affects how the data is sent. Apparently the data is sent as UTF-8 encoded, since the problem was in reading it server-side. So adding the meta
(though OK) has no effect on this.
Upvotes: 0
Reputation: 111219
It should work if you define the charset in the Content-Type
request header. I believe it is usually not defined by default. For example if you use jQuery to make the request you have to add "charset=utf-8" to the contentType
option: http://api.jquery.com/jQuery.ajax/
There's no way to force a web browser to send Content-Type for every request, so it's better call setCharacterEncoding
always.
Upvotes: 2