Mixing HTML with JSP Code

The following example illustrates mixing HTML with JSP code. For starters, typically some of the JSP programs look weird and confusing because both the HTML and JSP code is mixed. But if you can carefully understand what tag is what, you can write even more complex JSP programs than you see.
       One key point you need to know that, you can end a scriptlet tag anywhere you want. For example,

<%
if(true){
%>
<p>This is in the if-block. </p>
<%
out.println("This is that's it.");
}
%>

As you can see the first tag <% %> was ended unexpectedly to  write the message This is in the if-block. This is a html line executed only if the if-condition is met. Isn't this simple?
Now let us write a program.

index.jsp

<%!
int a=10;
%>
<%
    if(a==10)
    {
%>
<b>This is bold text, I'm seen only when a==10</b>
<%
    }
    else
    {
%>
<b>I am not seen, because a is not 10!</b>
<%
    }
%>
<br/>I am after if-else.
Previous: Using scriptlets in JSP
Next: Using declarations with example


Posted by at on

Tags: Java Server Pages,