3

发生了一些有趣的事情。当我调用一个 url abc.com时,一个 servlet 被寻址,但请求通过一个过滤器(到 servlet)。从 servlet 我尝试将请求分派到jsp页面。但是当我尝试分派请求时,我得到一个抛出的异常:java.lang.IllegalStateException: Exception occurred when flushing data

过滤代码

public class FirstSiteFilter implements Filter {
private FilterConfig fc;

@Override
public void init(FilterConfig config) throws ServletException {
    this.fc = config;
}

@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);
}

@Override
public void destroy() {

}   
}

Servlet 代码(请求从过滤器链接到此 Servlet)

public class FW_FirstSite extends HttpServlet {


@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);
    // THE FOLLOWING TWO STATEMENTS CAUSE AN EXCEPTION
    // IF I COMMENT THEM OUT NO EXCEPTION IS THROWN BUT AN EXCEPTION IS THROWN IF I LEAVE THEM AS IT IS
    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
    rd.forward(request,response);
}
}

为什么我会遇到这种类型的异常?相反,如果我将语句放在它起作用的语句response.sendRedirect("...")之后。request.setAttribute("client IP",IP)

编辑(web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
    <servlet-name>guestbook</servlet-name>
    <servlet-class>guestbook.GuestbookServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>emailtest</servlet-name>
    <servlet-class>Servlets.emailtest</servlet-class>
</servlet>
<servlet>
    <servlet-name>ValidateCredentials</servlet-name>
    <servlet-class>Servlets.ValidateCredentials</servlet-class>
</servlet>
<servlet>
    <servlet-name>UploadImagesToAisle</servlet-name>
    <servlet-class>Servlets.UploadImagesToAisle</servlet-class>
</servlet>
<servlet>
    <servlet-name>HandleSongLink</servlet-name>
    <servlet-class>Servlets.HandleSongLink</servlet-class>
</servlet>
<servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>Servlets.context</servlet-class>
</servlet>
<servlet>
    <servlet-name>HandleVideoLink</servlet-name>
    <servlet-class>Servlets.HandleVideoLink</servlet-class>
</servlet>
<servlet>
    <servlet-name>HandlePoetry</servlet-name>
    <servlet-class>Servlets.HandlePoetry</servlet-class>
</servlet>
<servlet>
    <servlet-name>displaykey</servlet-name>
    <servlet-class>Servlets.displaykey</servlet-class>
</servlet>
<servlet>
    <servlet-name>tester_writeXML</servlet-name>
    <servlet-class>Servlets.tester_writeXML</servlet-class>
</servlet>
<servlet>
    <servlet-name>ShowBlob</servlet-name>
    <servlet-class>Servlets.ShowBlob</servlet-class>
</servlet>
<servlet>
    <servlet-name>FW_FirstSite</servlet-name>
    <servlet-class>Servlets.FW_FirstSite</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>guestbook</servlet-name>
    <url-pattern>/guestbook</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>sign</servlet-name>
    <servlet-class>guestbook.SignGuestbookServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>sign</servlet-name>
    <url-pattern>/sign</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>HandleSubmittedPoem</servlet-name>
    <servlet-class>Servlets.HandleSubmittedPoem</servlet-class>
</servlet>
<servlet>
    <servlet-name>HandleSubmittedMessage</servlet-name>
    <servlet-class>Servlets.HandleSubmittedMessage</servlet-class>
</servlet>
<servlet>
    <servlet-name>HandleSongDedicated</servlet-name>
    <servlet-class>Servlets.HandleSongDedicated</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HandleSubmittedPoem</servlet-name>
    <url-pattern>/HandleSubmittedPoem</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>HandleSubmittedMessage</servlet-name>
    <url-pattern>/HandleSubmittedMessage</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>HandleSongDedicated</servlet-name>
    <url-pattern>/HandleSongDedicated</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>emailtest</servlet-name>
    <url-pattern>/emailtest</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ValidateCredentials</servlet-name>
    <url-pattern>/ValidateCredentials</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>UploadImagesToAisle</servlet-name>
    <url-pattern>/UploadImagesToAisle</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>HandleSongLink</servlet-name>
    <url-pattern>/HandleSongLink</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>context</servlet-name>
    <url-pattern>/context</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>HandleVideoLink</servlet-name>
    <url-pattern>/HandleVideoLink</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>HandlePoetry</servlet-name>
    <url-pattern>/HandlePoetry</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>displaykey</servlet-name>
    <url-pattern>/displaykey</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>tester_writeXML</servlet-name>
    <url-pattern>/tester_writeXML</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>ShowBlob</servlet-name>
    <url-pattern>/ShowBlob</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>FW_FirstSite</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>FirstSiteFilter</filter-name>
    <filter-class>Filters.FirstSiteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>FirstSiteFilter</filter-name>
    <servlet-name>FW_FirstSite</servlet-name>
</filter-mapping>



<!--
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
-->

</web-app>
4

1 回答 1

1

RequestDispatcher 总是需要一个正斜杠。所以尝试将其更改为以下

RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");

当您使用 RequestDispatcher.forward 调用另一个 servlet 时,它是“命令链”。

当第一次调用 servlet 时,响应对象是全新的:它的头没有设置,它的缓冲区是空的,并且没有数据被写入客户端。

但是,一旦状态代码或任何标头已写入(或可能已写入)到客户端,或者当数据已(或可能已)写入主体流时,您就可以容易受到 IllegalStateException 错误的影响。此异常发出信号的问题是,您正在(或可能正在)写入的新数据与已经设置然后不可挽回地发送(“提交”)到客户端的数据不一致。

此异常的两个常见变体是“java.lang.IllegalStateException:标头已发送”和“java.lang.IllegalStateException:无法转发,因为已获得输出流或写入器”。

“标头已发送”表示一个或多个标头已提交给客户端,因此您无法再次设置该标头。

“Output Stream or Writer has already been getting”表示由于调用 servlet 已经调用了 response.getWriter() 或 response.getOutputStream(),这会污染数据流,因为响应已经(或可能已经)写入已经,使其不适合转发。

于 2012-09-08T03:57:40.833 回答