2

In PHP, when we want to stop the page processing, we can use the die() or exit() function. How to do the same thing in jsp?

EDIT I am using it to redirect to other page if the user is accessing a forbidden page (i.e those page that requires login or pages that is only accessible by specific IP addresses in the whitelist). In PHP, I am doing it like this:

<?php 
    ...
    if($logged_in && $ip_isValid){
       // display page
    }else{
       // redirect to other page
    }
    ...

or for easier reading/coding/development, I can use PHP's die() like this:

<?php
    ...
    if(!$logged_in || !$ip_isValid){
       // redirect to other page or do some processing
       die();
    }
    // else is not needed anymore here
    // normal page execution
    ...

So if there is no equivalent for die() or there is a better way of doing it, please tell me what can I do to achieve the same thing in JSP.

someone
  • 23
  • 3
  • There is no reason to have something like die() in Java/JSPs. JSPs should be used as a pure view technology. It should just generate HTML. Not try to access a database, read files or whatnot. That is the job of a controller, written in Java. If the controller has such a critical problem, it will throw an exception, and the JSP won't even be executed. If your JSP contains Java code, you're doing it wrong. See http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – JB Nizet Apr 10 '16 at 08:41
  • You didn't answer my question directly BUT this is definitely the answer I am looking for. I am new in JSP and wondering how to preprocess each page just like what I always do in PHP. By your comment, I had realized that JSP is really powerful. Thank you so much. I will edit my question for you to incorporate your comment as an answer. You really deserve the green check. And of course I believe that someone might have the same question with me but looking for your answer as well. – someone Apr 11 '16 at 00:49

0 Answers0