“HttpServletResponse已提交”的内容是什么?意思? [英] What does "HttpServletResponse is committed" mean?

查看:187
本文介绍了“HttpServletResponse已提交”的内容是什么?意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所述,提交HttpServletResponse是什么意思?

As stated in title, what does it mean that HttpServletResponse is committed?

我有一些请求拦截器,扩展 HandlerInterceptorAdapter ,覆盖 postHandle 方法。 post handle方法接受参数 final HttpServletResponse response 。在方法体中有一个如果语句检查 response.isCommitted(),那究竟是什么检查?

I have some request interceptor, extending HandlerInterceptorAdapter, that overrides postHandle method. Post handle method takes parameter final HttpServletResponse response. In method body there is an if statement checking if response.isCommitted(), what exactly does that check?

private static final String voidResponse = "null";

@Override
    public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
            final ModelAndView modelAndView) throws IOException {
        if (!response.isCommitted()) {
            if (DefaultServletHttpRequestHandler.class == handler.getClass()) {
                return;
            }
            response.setStatus(200);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            try (final Writer writer = response.getWriter()) {
                writer.write(voidResponse);
            }
            log.info("void method called, respond with 200 and null");

            response.flushBuffer();
        }
    }


推荐答案

ServlerResponse.isCommited()检查响应是否已经提交给客户端(意味着servlet输出流已经打开以写入响应内容)。

ServlerResponse.isCommited() checks if the response has been already committed to the client or not (Means the servlet output stream has been opened to writing the response content).

提交的响应包含HTTP状态和标头,您无法修改它。
还需要注意的是,在这种情况下,响应内容尚未编写,因为标题和状态是在内容本身之前提交的。

The committed response holds the HTTP Status and Headers and you can't modify it. It's important also to note that in this case the response content has NOT been written yet, as the headers and status are committed before the content itself.

在您的示例中,需要进行检查以防止响应已经提交但有人试图修改它的情况,在这种情况下您将获得 IllegalStateException 说明已经提交了响应。

In such examples as yours the check is required to prevent situations when the response has already been commited but someone is trying to modify it, in which case you will get an IllegalStateException stating that response has already been committed.

UPDATE :我看到你正在使用Spring控制器。这个故事有点不同。

UPDATE: I see that you are using Spring controllers. The story differs a bit here.


  • 案例1 :如果您使用 @ResponseBody 在你的控制器方法中或返回 ResponseEntity Spring在调用 postHandle()之前写入并提交响应,因此无法在以后更改响应。在这种情况下说 response.isCommited()语句将始终返回true并且您无法修改响应。

  • 案例2 :如果你没有上面提到的注释而且没有返回 ResponseEntity 或者控制器返回NULL postHandle ()拦截器的方法在处理完控制器方法后被调用,但响应尚未提交。这意味着您可以根据需要修改响应(例如,返回200 OK)。

  • Case 1: If you are using @ResponseBody in your controller method or returning ResponseEntity Spring writes to and commits the response before the postHandle() is called, which makes it impossible to change the response later. That said in this case response.isCommited() statement will always return true and you are not able to modify the response.
  • Case 2: If you don't have the above mentioned annotation and don't return ResponseEntity or controller returns NULL the postHandle() method of interceptor is called after the controller method has been processed, but the response has not been committed yet. This means you can modify the response as you want (e.g. return 200 OK).

这篇关于“HttpServletResponse已提交”的内容是什么?意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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