java - How to read right a utf-8 string in the serlvet? -


this question has answer here:

before mark question duplicate this question, want read , post.

however, sth wrong , continue read in wrong format data form in jsp (with post method). have done:

1. in jsp, have put this

<%@page contenttype="text/html" pageencoding="utf-8" language="java" %> 

and in header <meta charset="utf-8">

2. in servlet

protected void processrequest(httpservletrequest request, httpservletresponse response)             throws servletexception, ioexception {          //...                 //code         //...          request.setcharacterencoding("utf-8");          /*if (request.getcharacterencoding() == null) {                 request.setcharacterencoding("utf-8");          }        */        //...        //code        //...         s1 = request.getparameter(kname1); //<-here read value jsp , ÎÏδÏÎ±Î´Î±Ï   } 

3. in web.xml have <?xml version="1.0" encoding="utf-8"?>

what missed here??

is html right?

you seem using html5

<%@page contenttype="text/html" pageencoding="utf-8" language="java" %><!doctype html> <html>   <head>     <meta charset="utf-8">     <title>...</title>   </head>   <body>     <form accept-charset="utf-8" ...>     ...   </body> </html> 

the indication in form tag allows sending utf-8 input unescaped server. otherwise might &#8085; or such. (this seems not case.)

you might check things, like

<input name="test" type="hidden" value="\u0109ĉ"> 

should give "ĉĉ".

there difference between forms method post , get, check them both before fixing encoding functionality in stone.

in server request encoding?

check request.getcharacterencoding() == null. otherwise filter might interfere. setting of encoding can done initially. maybe servlet forwarded to, or whatever.

conversion might happen anywhere, when looking @ text. dump request parameter purely possible:

string s = request.getparameter("test"); (char ch : s.tochararray()) {     printf("\\u%04x ", 0xffff & (int)ch); } println(); 

Comments