发生了一些有趣的事情。当我调用一个 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>