java - Encoding UTF-8 in HTTPServlet request -
this may like problem that's been solved it's not, because have gone through questions deal utf-8 , none of solutions has helped me.
i'm sending http request java servlet containing json object using json simple library.
- i added utf-8 encoding in tomcat xml file
- my html pages support utf-8 encoding
- both database , tables utf-8 encoded
- i changes default encoding of jvm utf-8 using system variables (yeah! that's how desperate got)
this dispatcher function:
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.setcharacterencoding("utf-8"); ajaxparser cr = ajaxparser.clientrequestfactory(); clientrequest msg = cr.parseclientajax(request); handlerequest hr = new handlerequest(); handlerequeststatus hrs = hr.handlemessage(msg); ajaxresponsegenerator arg = new ajaxresponsegenerator(); jsonobject jsonobj = arg.handleresponse(hrs); response.setcharacterencoding("utf-8"); response.setcontenttype("application/json"); printwriter out = response.getwriter(); system.out.println(jsonobj);// write json object console out.println(jsonobj); }
and how parsing string:
public clientrequest parseclientajax(httpservletrequest request) { clientrequest msg = new clientrequest(); stringbuffer jb = new stringbuffer(); string line = null; try { bufferedreader reader = request.getreader(); while ((line = reader.readline()) != null) jb.append(line); } catch (exception e) { e.printstacktrace(); } jsonparser parser = new jsonparser(); try { jsonobject obj = (jsonobject) parser.parse(jb.tostring()); string opcodestring = (string) obj.get("opcode"); requestcodeenum numericenumcode = (requestcodeenum) opcodesmap .get(opcodestring); msg.setopcode(numericenumcode); string entitystr = obj.get("entity").tostring(); entity entity = makeentityfromstring(numericenumcode, entitystr); msg.setentity(entity); } catch (parseexception pe) { system.out.println(pe); } return msg; }
i tried debugging printing eclipse console (which changed utf-8 encoding) text send throughout application find out text not encoded correctly, found text in right encoding until right before execution of query. after check database manually , text inserted there question marks.
i tried manually insert non-english text database using workbench, , works fine, both in database , when displaying data in html afterwards. problem happens when insert data web page.
i'm stuck, have no idea problem might be.
any suggestions?
try this:
inputstream inputstream = request.getinputstream(); bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream , standardcharsets.utf_8));
Comments
Post a Comment