興味深いことが起こっています。URL abc.comを呼び出すと、サーブレットがアドレス指定されますが、リクエストはフィルターを通過します (サーブレットに)。サーブレットからリクエストを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() {
}
}
サーブレット コード(要求はフィルターからこのサーブレットにチェーンされます)
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>