如何阻止JSP執行?Java:JSP在頁面的其餘部分暫停執行
我有JSP通過「轉發」方式將用戶踢出頁面。
public boolean kickIfNotLoggedIn(
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//code to check if user is logged in
req.getRequestDispatcher(
ACCESS_DENIED_PAGE).forward(request, response);
}
在我的JSP中,我有這樣的代碼,在任何HTML輸出:
<%
//loginHelper.kickIfNotLoggedIn(request, response);
if (!loginHelper.kickIfNotLoggedIn(request, response)) {
return;
}
%>
如果我不使用return語句時,JSP繼續處理,我也得到一個NullPointerException。如果我使用return語句(如在在網上各種來源通常建議),我得到一個IllegalStateException:
StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response
at org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:717)
at org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacade.java:226)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:187)
任何想法如何解決這個問題,或者另一種方式來實現訪問被拒絕的頁面?
謝謝
感謝您的回答,您是否可以指示我如何爲我的JSP創建Servlet過濾器?非常感激。 – bguiz 2010-06-02 11:08:09
@bguiz:http://courses.coreservlets.com/Course-Materials/msajsp.html – BalusC 2010-06-02 11:41:46
@BalusC:謝謝,這是一套非常好的文章鏈接! – bguiz 2010-06-02 12:27:26