【问题标题】:JSP servlet include output tuncatedJSP servlet 包含截断的输出
【发布时间】:2015-11-07 03:12:38
【问题描述】:

我正在尝试在 jsp 页面中包含来自 servlet 的响应:

<li><jsp:include page="/icr/TasksServlet.html"/></li>

servlet 返回一个词:

try (PrintWriter out = response.getWriter()) {
        out.println("OOps");
        out.flush();
        out.close();
    }

结果是我的页面在“OOps”之后直接被截断

    <ul class="navList">
    <li>ICR Open Tasks</li>
    <li>
        <ul>
    <li>OOps

tomcat 抛出这个错误:

org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet 
[validateUser] in context with path [/CAS] threw exception
[java.lang.IllegalStateException: Exception occurred when flushing data]
with root cause
java.io.IOException: Stream closed

我尝试添加和删除 out.flush();和 out.close();以及在 jsp:include 标记中添加了 flush="true"。

我运行tomcat 8.0.24;带有 Spring MVC 的 Netbeans。

编辑:它有点类似于reported bug,但它已在 tomcat 8.0.18 版以后修复。明天我将在 glassfish 服务器上运行代码,看看它是否是服务器问题。

【问题讨论】:

    标签: java jsp tomcat servlets


    【解决方案1】:

    关闭 servlet 中的 HttpServletResponse 可防止 JSP 页面写入响应。 try with resources 块也会在完成时关闭 HttpServletResponse。

    使用普通的 try 语句,不要关闭响应。

    【讨论】:

    • 这是有道理的。我明天一回来工作就试试。
    • 是的,我删除了 try with resource 和 close(),这使它工作。谢谢!
    猜你喜欢
    • 2016-05-29
    • 2014-03-25
    • 2011-06-26
    • 2014-09-04
    • 2012-11-09
    • 1970-01-01
    • 2011-03-25
    • 2016-04-15
    相关资源
    最近更新 更多