问题描述
发生了一些有趣的事情.当我调用url abc.com 时,将处理一个servlet,但请求将通过过滤器(到达servlet).从servlet我尝试将请求分派到 jsp 页面.但是,当我尝试分派请求时,我抛出了一个异常:java.lang.IllegalStateException: Exception occurred when flushing data
There is something interesting going on. When I call a url abc.com a servlet is addressed but the request passes through a filter (to the servlet). From the servlet I try to dispatch the request to a jsp page.But as I try to dispatch the request I get an exception thrown which is : 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)
Servlet code (request is chained to this servlet from the filter)
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)
之后,它将起作用.
Why is it that I get an exception of this type ? If instead I put the statement response.sendRedirect("...")
after the statement request.setAttribute("client IP",IP)
it works.
编辑(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>
推荐答案
RequestDispatcher始终需要正斜杠.因此,尝试将其更改为以下内容
RequestDispatcher always needs a forward slash. So try changing it to the following
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
当您使用RequestDispatcher.forward调用另一个servlet时,它就是命令链".
When you use RequestDispatcher.forward to call another servlet, it is "chain of command."
第一次调用servlet时,响应对象是新鲜的:它的头尚未设置,其缓冲区为空,并且没有数据写入客户端.
When a servlet is first called, the response object is fresh and new: its headers have not been set, its buffers are empty, and no data has been written to the client.
但是,一旦状态码或任何标头已被写入(或可能被写入)客户端,或者当数据已被(或可能已被写入)主体流时,那么您可能会遇到IllegalStateException错误.此异常表示发出信号的问题是,您正在(或可能正在)写入的新数据与已设置的数据不一致,然后将其不可撤消地发送(提交")给客户端.
However, as soon as either the status code or any of the headers have been written -- or potentially written -- to the client, or when data has been -- or may have been -- written to the body stream, then you may be susceptible to the IllegalStateException error. The problem that this exception is signalling is the new data that you are (or may be) writing is inconsistent with the data that's already been set and then irretrivably sent ("committed") to the client.
此异常的两个常见变体是"java.lang.IllegalStateException:标头已发送"和"java.lang.IllegalStateException:由于输出流或写入器已被获取而无法转发".
Two common variants of this exception are "java.lang.IllegalStateException: Header already sent" and "java.lang.IllegalStateException: Cannot forward as Output Stream or Writer has already been obtained".
标头已发送"表示一个或多个标头已提交给客户端,因此您无法再次设置该标头.
"Header already sent" means that one or more headers have been committed to the client, so you can't set that header again.
已经获得输出流或Writer"意味着,由于调用servlet已经调用response.getWriter()或response.getOutputStream(),由于响应已经(或可能已经被响应)污染了数据流. )已写入,因此不适合转发.
"Output Stream or Writer has already been obtained" means that since the calling servlet has already called response.getWriter() or response.getOutputStream(), that contaminates the data stream, since the response has been (or may have been) written to already, making it unsuitable for forwarding.
这篇关于刷新数据时发生异常.这是什么,为什么我要得到这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!