How to stop processing a JSP early?

后端 未结 5 1164
失恋的感觉
失恋的感觉 2021-01-18 10:47

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 wan

相关标签:
5条回答
  • 2021-01-18 11:34

    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.

    0 讨论(0)
  • 2021-01-18 11:40

    You may use JSTL tag:

    <c:if test="condition">
       bodytext
    </c:if>
    

    Which is better than <% if(condition) { %> bodytext <% } %> for controlling on loading page / process.

    0 讨论(0)
  • 2021-01-18 11:41

    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.

    0 讨论(0)
  • 2021-01-18 11:41

    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.

    0 讨论(0)
  • 2021-01-18 11:42

    Since 2.0, there is a standard way:

    throw new javax.servlet.jsp.SkipPageException();
    
    0 讨论(0)
提交回复
热议问题