处理ajax请求并发送响应java servlet

时间:2012-11-13 18:21:29

标签: java ajax servlets response

我有一个ajax请求函数(用JavaScript编写)和Java Servlet来处理这个请求。我需要获取所有请求参数,如果成功,则发回确认它已成功。我如何发送确认信息?作为属性,如{isSuccess: true}。我的代码:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
    IOException {
    Enumeration<?> paramNames = request.getParameterNames();
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        // storing data fn
    }
    // how to send back a message here?
}

1 个答案:

答案 0 :(得分:1)

通过致电PrintWriter获取HttpServletResponse#getWriter个对象并撰写您的String

response.getWriter().write("{isSuccess: true}");