在HTTPServlet请求中编码UTF-8 [英] Encoding UTF-8 in HTTPServlet request

查看:209
本文介绍了在HTTPServlet请求中编码UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来像一个已经解决的问题,但事实并非如此,因为我已经完成了所有与UTF-8相关的问题,但没有一个解决方案对我有所帮助。

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.

我正在使用JSON简单库向包含JSON对象的java servlet发送http请求。

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


  1. 我在Tomcat xml文件中添加了UTF-8编码

  2. 我的HTML页面支持UTF- 8编码

  3. 我的数据库和我的所有表都是UTF-8编码的

  4. 我使用JVM的默认编码更改为UTF-8系统变量(是的!这是我多么绝望)

  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)

这是我的调度员功能:

    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);

}

这就是我解析String的方法:

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;
}

我试图通过打印到Eclipse控制台进行一些调试(我也是改为UTF-8编码)我在整个应用程序中发送的文本,以找出文本未正确编码的位置,我发现文本处于正确的编码状态,直到我的查询执行之前。之后我手动检查数据库并将文本作为问号插入。

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.

我尝试使用Workbench手动将非英文文本插入我的数据库,它可以正常工作很好,在数据库本身和之后在我的HTML中显示数据时都是如此。
只有当我从网页插入数据时才会出现问题。

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.

有任何建议吗?

推荐答案

试试这个:

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

这篇关于在HTTPServlet请求中编码UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆