使用表单时数据出现乱码

When i submit a form using form.submit(), non-english chars (say chinese, korean) in stored as garbled in database.

I have using the following code in jsp:

<%
 response.setContentType("text/html;charset=utf-8");
 response.setCharacterEncoding("UTF-8");
%>
<html>
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
 <body>
 <form name="TestForm" enctype="application/x-www-form-urlencoded" accept-charset="UTF-8" action="<%=formaction%>" method="POST">
  <table>
   <tr><td>Source</td><td>:</td>
   <td><input type="text" name="SOURCE"> </td></tr>

   <tr><td>Category</td><td>:</td>
   <td><input type="text" name="CATEGORY"> </td></tr>

   <tr><td>Message</td><td>:</td>
   <td><input type="text" name="MESSAGE"> </td></tr>
   <tr><td><input type="submit" name="Submit" value="Ok"></td></tr>
  </table>
 </form>
 </body>
 </html> 

I am setting request options like Content-type, CharSet when i send Ajax request. It's working as expected in Ajax, but even i set those attributes in form, it is not working on form.submit().

Try fetching the value on the server side using the following code. Ensure character encoding is set prior to getParameter

request.setCharacterEncoding("UTF-8"); 
String str = request.getParameter("fuzz-field");

Also check if your server has URIEncoding set to UTF-8

Hope this helps

Here is the sample code that I tried and got it working

JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8 %> 

  <form id="form-sample" name="form-sample" method="post" action="/HTML5Sample/sampleservlet">
<!--field has the value of two Chinese characters.-->
<input type="text" id="fuzz-field" name="fuzz-field" value="中文"/>
<input type="submit" value="Submit">
</form>                                                                                              

Tomcat Server.xml

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

Servlet class -

request.setCharacterEncoding("UTF-8"); 
String str = request.getParameter("fuzz-field");
System.out.println("character field " + str);    

To view this on eclipse console you will need to modify the console settings - Window > Preference > encoding to utf-8

Thanks for your comments. I have resolved this issue.I have used a custom filter without using characterEncodingFilter since this problem occurred. After added this filter and configured in WEB-INF/web.xml, everything is works fine.

For more information refer this wiki: http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8