How to stop processing a JSP early?

Regardless of how to terminate processing of a JSP early, I would suggest handling flow control in a servlet prior to handling the display, and switch to an appropriate JSP depending on the outcome of the function call.

I tend to use JSPs purely for displaying the contents of a bean (or more than one), and all logic is contained within the invoking servlet. By doing this you get a much cleaner separation of concerns between your components, and a more easily comprehensible and debuggable system.


Since 2.0, there is a standard way:

throw new javax.servlet.jsp.SkipPageException();

Brian's answer is a good advice, but it does not answer the question clearly.

The answer is YES. Bulk of JSP is converted to a method _jspService, which is being run when JSP is "executed", and since it is a regular function, that does cleanup in finally block, it is safe to return.

Tags:

Java

Jsp