tags:

views:

254

answers:

2

Hi,

I've got a JSP page, which calls a function and checks its return value. If the return value is not null, the JSP page goes on to use it. If the return value IS null, I want the processing of the JSP page to stop (this is because the function will perform a redirect before returning the null object. Also, the rest of the JSP code obviously uses this object, and will get a null pointer exception if the function returned null).

So my question is, what is the right way of stopping the load of a JSP page? Can I do something like this:

if (Func() == null) { return; }

Or is using a return in the middle of a JSP not the cleaner way to go about this?

Thanks

+5  A: 

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.

Brian Agnew
This would probably be the best thing to do, but I can't change the current architecture of the project I'm working on (not worth the time right now).
Edan Maor
A: 

If you really really want to do this I would suggest something like the following


..some jsp..
<%
   if (iWantToKeepProcessing) {
%>
..someother stuff..

<%
}
%>

I have to agree with Brian though and say that you should avoid entering the JSP if possible. This might not be available to you if you are handling a post/get directly targeting a dynamic JSP file rather than a controller/servlet.

Shaun F