Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting POST form data in UTF -8

Tags:

java

html

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?

like image 631
Akshay Avatar asked Feb 06 '12 09:02

Akshay


1 Answers

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.

like image 74
Joni Avatar answered Oct 05 '22 08:10

Joni