我有一个 tomcat6 servlet,它以这种方式管理传入的 HttpPosts:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (request.getParameter("cmd") != null) {
eventPool.addEvent(new CommandEvent(new String[] { request.getParameter("cmd"),
request.getParameter("json") }, response));
}
}
现在将处理该请求。完成后,我想以这种方式将结果写入请求客户端:
protected void sendResponse(HttpServletResponse httpResponse, String content){
try {
httpResponse.getWriter().println(CMD + "#" + content);
httpResponse.getWriter().close();
} catch (IOException e) {
e.printStackTrace();
}
}
但它无法刷新,我得到一个 NullPointerException 因为 HttpResponse 已经关闭。
如何防止 HttpResponse 在我想要之前刷新?