java.lang.IllegalStateException:刷新数据时发生异常

时间:2012-09-07 13:30:12

标签: java servlets servlet-filters illegalstateexception

  

可能重复:
  Exception occurred when flushing data . What is this and why am I getting this?

以下是过滤器的摘录。它获取客户端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设置的属性并对其进行处理。

首次打开网站 foo.com 时,首先要运行servlet。但是当我打开网站时,我看到了这个堆栈跟踪:

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); 和at Filters.FirstSiteFilter.doFilter(FirstSiteFilter.java:24),这是语句 chain.doFilter(request,response);

为什么要获得这些例外?

2 个答案:

答案 0 :(得分:1)

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

允许多次设置您的回复内容类型而不重要,但除非您已完成预处理,否则不应致电编写者。

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

答案 1 :(得分:0)

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

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

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