3

可能重复:
刷新数据时发生异常。这是什么,为什么我会得到这个?

以下是过滤器的片段。它获取客户端 IP,设置属性,然后将请求链接到 servlet。

@Override
 public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) 
        throws ServletException,IOException {
    String IP = request.getRemoteAddr();
    request.setAttribute("client IP from the filter", IP);
    chain.doFilter(request, response);
}

以下是已收到过滤请求的 servlet 的片段。它获取 IP,将其存储为另一个属性,然后将请求分派给index.jsp

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String IP = (String)request.getAttribute("client IP from the filter");
    request.setAttribute("Client IP", IP);
    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
    rd.forward(request,response);
}

index.jsp ,然后检索 servlet 设置的属性并对其进行处理。

servlet 是第一次打开网站foo.com时运行的第一件事。但是当我打开网站时,我看到了这个堆栈跟踪:

java.lang.IllegalStateException: Exception occurred when flushing data
at com.google.appengine.runtime.Request.process-d6995d0c305e239e(Request.java)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:191)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
at org.apache.jasper.runtime.JspFactoryImpl.access$100(JspFactoryImpl.java:40)
at org.apache.jasper.runtime.JspFactoryImpl$PrivilegedReleasePageContext.run(JspFactoryImpl.java:166)
at java.security.AccessController.doPrivileged(AccessController.java:34)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:139)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
at Servlets.FW_FirstSite.doGet(FW_FirstSite.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at Filters.FirstSiteFilter.doFilter(FirstSiteFilter.java:24)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:452)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:458)
at com.google.tracing.TraceContext.runInContext(TraceContext.java:698)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:336)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:328)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:456)
at java.lang.Thread.run(Thread.java:679)
Caused by: java.io.IOException: Stream closed
at org.apache.jasper.runtime.JspWriterImpl.ensureOpen(JspWriterImpl.java:204)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:115)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:188)
... 43 more

日志表明异常发生在Servlets.FW_FirstSite.doGet(FW_FirstSite.java:27)这是语句rd.forward(request,response); Filters.FirstSiteFilter.doFilter(FirstSiteFilter.java:24)这是语句chain.doFilter(request, response); .

为什么会得到这些异常?

4

2 回答 2

1

我的猜测是,您对 response.getWriter() 的调用会在刷新响应时启动将响应写入客户端。出于这个原因,您不能再转发请求,因为它已经被刷新了。

可以多次设置响应内容类型而不用担心,但除非您已经完成了预处理,否则您不应该调用作者。

另请注意,调用 getOutputStream() 具有相同的效果。

于 2012-09-07T14:41:34.163 回答
0

doGet 方法中以下代码的目的是什么?

response.setContentType("text/plain");
PrintWriter writer = response.getWriter();

我实际上并没有检查规范,但我怀疑您是否可以在将请求转发到另一个资源之前操纵 servlet 的响应(既不能通过设置标头,也不能通过访问 OutputStream 或 Writer)。

于 2012-09-07T14:07:55.167 回答