Link to home
Start Free TrialLog in
Avatar of 99525
99525

asked on

How to stop the output in a .jsp file

Can I stop the following contents in a .jsp file when certain condition is met? Such as:

<%
  if (condition == true)
    Stop the output
%>

Following are the other contents in this page.

(These contents shouldn't be displayed if the condition = true.)
Avatar of kotan
kotan
Flag of Malaysia image

Yes, you can.That is a correct method.
If you want to disabled html contains. Try this,

<%
if (condition == true) {
%>
html things.
<%
}
%>


Is that what you want?
Avatar of 99525
99525

ASKER

This can end the output. But this is not what I want.
Because the "{" and the "}" are seperated by many comlicated Html mess. It's not very clear and easy to maintain.
Is there any usages in JSP like output.end or output.finish?
There is no way to do that.
I think it is do able
There are two ways to solve that:

1-
<%
  if (condition==true){
%>
  <input type="text"> or any HTML code
<%
  }else{
%>
  <HTML CODE>
<%
  }
%>

2-
<%
  PrintWriter out=response.getWriter();
  if (conditon==true){
       out.println("<HTML>");
   }else{
       out.println("any html code");
  }
%>
ASKER CERTIFIED SOLUTION
Avatar of pellep
pellep
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this

<%
if(cond==true){
response.sendRedirect("a.html");
}
else{
response.sendRedirect("b.html");
%>

Note: a.html is the page that display all your output.
      b.html is the empty page.

hope it helps.
Avatar of 99525

ASKER

pellep's comment is close to the solution what I want.

sushifish's comment also works. But sendRedirect is related to HTTP header. One should be careful if he uses this method.

return; will ignore remaining HTML tags, and makes them unmatch. I think current browsers can deal with this.
So I will accept pellep's answer.

Thank you.