title: Java: JSP halt execution on rest of page
body: <p>How do you stop a JSP from executing?</p>
  
  <p>I have JSPs which kick the user off a page by means of a "forward".</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>In my JSP, I have this code, BEFORE any HTML output:</p>
  
  <pre><code>&lt;%
              //loginHelper.kickIfNotLoggedIn(request, response);
              if (!loginHelper.kickIfNotLoggedIn(request, response)) {
                  return;
              }
  %&gt;
  </code></pre>
  
  <p>If I don't use the return statement, the JSP continues processing, and I get a NullPointerException. If I use the return statement (as is commonly suggested on various sources on the net), I get an IllegalStateException:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Any ideas how to fix this, or another way to achieve an access denied page?</p>
  
  <p>Thanks</p>