Java: JSP halt execution on rest of page

Posted by bguiz on Stack Overflow See other posts from Stack Overflow or by bguiz
Published on 2010-06-02T10:36:31Z Indexed on 2010/06/02 10:43 UTC
Read the original article Hit count: 260

Filed under:
|
|
|

Hi,

How do you stop a JSP from executing?

I have JSPs which kick the user off a page by means of a "forward".

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);
}

In my JSP, I have this code, BEFORE any HTML output:

<%
            //loginHelper.kickIfNotLoggedIn(request, response);
            if (!loginHelper.kickIfNotLoggedIn(request, response)) {
                return;
            }
%>

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:

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)

Any ideas how to fix this, or another way to achieve an access denied page?

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about jsp