从servlet发送值到javascript错误(日文字符)

时间:2015-08-21 08:02:50

标签: javascript java character-encoding special-characters

当字符串“ああああ”变成“????”时,我收到错误然后将数据从servlet传递给javascript。

controller .java

android:calenderViewShown="false"

的javascript

@RequestMapping(value = "searchbook", method = RequestMethod.POST, produces = "text/plain")
    public @ResponseBody String myController(HttpServletRequest request) throws SQLException {
        String myItem = request.getParameter("searchid");

        PostgrConnect db = new PostgrConnect();
        ResultSet rs;
        Book book = new Book();

        try {
            rs = db.getData("select * from mt_book where book_id='" + myItem + "'");
            while (rs.next()) {
                book.setBook_id(rs.getString("book_id"));
                book.setBook_title(rs.getString("book_title"));
                book.setAuthor_name(rs.getString("author_name"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublication_day(rs.getString("publication_day"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }

        return book.getBook_id() + ";" + book.getBook_title() + ";" + book.getAuthor_name() + ";" + book.getPublisher()
                + ";" + book.getPublication_day();
    }

我可以从数据库中获取数据:

http://imgur.com/fpibNPt

但是当传递给javascript时:

http://imgur.com/WhGwKwP

1 个答案:

答案 0 :(得分:1)

确保以下内容:

  1. Java文件编码为MS932

  2. 响应/请求编码为Shift_JIS

  3. 前端HTML使用charset定义的元标记为Shift_JIS

  4. 如果您使用的是Eclipse IDE,则对于Java文件编码,右键单击导航窗格中的文件名,然后单击“属性”。 Open Properties Dialog in Eclipse IDE

    选择“文本文件编码”>选择“其他”,然后键入MS932 Set Encoding

    这会奏效。 2年前,我在辉瑞日本工作,在开发阶段的最初阶段,我们面临着这些挑战。