ajax如何获取servlet的数据

servlet已经取出了数据,显示在html上
servlet:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Connection conn = DBConnectUtils.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;

    try {
        String sql = "select id,username from User";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();

        if (rs != null) {
            while (rs.next()) {
                 req.setAttribute("id", rs.getInt("id"));
                 req.setAttribute("username", rs.getString("username"));
                 req.getRequestDispatcher("showUser.html").forward(req,
                 resp);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DBCloseUtils.closeCSR(conn, pstmt, rs);
    }
}

ajax应该怎么写

这个你在循环里做的操作还是改改吧 ,另外 你要用ajax传值的话可以使用json字符串 吧要传的值转成json然后再传

你可以看看这个 http://www.cnblogs.com/BobCoder/p/6413463.html

JsonObject json=new JsonObject();
json.put("名字",值);
out(json.toString());
-----------------------后台代码------------------

以下为前端代码
eval (json)

json.名字 就是 值

你后台代码不能用setattribute,这样js是获取不到的,得用out存储,再用ajxa获取