java - HttpServletResponse.resetBuffer() 不起作用

标签 java google-app-engine servlets

在 Google 的 Appengine with Java 中,servlet:

@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Writer w=resp.getWriter();
    w.write("a");
    resp.resetBuffer();
    w=resp.getWriter();
    w.write("b");
}

在开发服务器的响应中给出“ab”。通过阅读 Servlet Spec 2.5,我被期望只是“b”。

怎么了?

我正在 https://cloud.google.com/appengine/docs/java/requests 阅读文档.

我尝试了 resp.setBufferSize(8192) 和 getBufferSize() 但只得到 1。

appengine 版本:1.9.34,java 版本:openjdk 版本“1.8.0_66-internal”,ubuntu 14.10

最佳答案

这是 Java Servlet 规范(3.0 版)的内容:

"The reset method clears data in the buffer when the response is not committed. Headers and status codes set by the servlet prior to the reset call must be cleared as well. The resetBuffer method clears content in the buffer if the response is not committed without clearing the headers and status code.

If the response is committed and the reset or resetBuffer method is called, an IllegalStateException must be thrown. The response and its associated buffer will be unchanged."

从表面上看,这表明 resetBuffer 应该清除 “a” 或抛出异常。

我想,您可以提出这样的论点,即字符在 Writer 中缓冲,而不是在响应缓冲区中缓冲,但 Servlet 规范没有做出任何此类区分。此外,这种对规范的解释会使 resetBuffer 变得毫无用处。

如果 Google AppEngine 确实按照您所说的方式运行,我会称之为错误。

关于java - HttpServletResponse.resetBuffer() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36691900/

相关文章:

python - 将图像上传到 S3 (boto + GAE)

java - Servlet 打印输出错误

Java Servlet 框架,可以执行将图像重新编码为首选格式等操作

java - 你能做一个可编辑的标签吗?

java - 如何使用 logback 禁用 accessExternalDTD 和 entityExpansionLimit 警告

java - 计算 catch block 中发生的异常数

eclipse - 删除@WebServlet 会导致 java.lang.ClassNotFoundException

java - 在 Java 中创建不扩展线程或实现 Runnable、Callable<V> 的线程、任务还有其他可能性吗?

google-app-engine - GoLang App Engine 结构名称

google-app-engine - 重复的属性索引如何在带有 ndb 的数据存储中工作