Safaa Eek
Safaa Eek

Reputation: 51

Encoding UTF-8 in HTTPServlet request

this may look like like a problem that's already been solved but it's not, because I have gone through all the questions that deal with UTF-8 and none of the solutions has helped me.

I'm sending http request to my java servlet containing JSON object using the JSON simple library.

  1. I added the UTF-8 encoding in Tomcat xml file
  2. my HTML pages support UTF-8 encoding
  3. both my database and all my tables are also UTF-8 encoded
  4. I changes the default encoding of the JVM to UTF-8 using system variables (yeah! that's how desperate I got)

this is my 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 the json object to console
    out.println(jsonObj);

}

and this is how I do the parsing to 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 to do some debugging by printing to the Eclipse console (which I also changed to UTF-8 encoding) the text I send throughout my application to find out where the text is not encoded correctly, I found that the text is in the right encoding until right before the execution of my query. after that I check the database manually and the text is inserted there as question marks.

I tried to manually insert Non-English text to my database using Workbench, and it works fine, both in the database itself and when displaying the data in my HTML afterwards. the problem happens only when I insert data from my web page.

I'm stuck, I have no idea where the problem might be.

Any suggestions?

Upvotes: 2

Views: 6593

Answers (1)

Pedro Stamm
Pedro Stamm

Reputation: 61

Try this:

InputStream inputStream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream , StandardCharsets.UTF_8));

Upvotes: 6

Related Questions