300

I want to output some HTML code based on some condition in a JSP file.

if (condition 1) {
    Some HTML code specific for condition 1
}
else if (condition 2) {
    Some HTML code specific for condition 2
}

How can I do that? Should I use JSTL?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
testndtv
  • 43,898
  • 91
  • 265
  • 396
  • 1
    Exampledepot has some nice examples. Have a look > http://exampledepot.8waytrips.com/egs/javax.servlet.jsp.jstl.core/if.html – darkapple May 09 '11 at 11:10
  • 1
    Possible duplicate of [How to use if-else option in JSTL](https://stackoverflow.com/questions/4587397/how-to-use-if-else-option-in-jstl) – Alex K Oct 16 '18 at 19:15

12 Answers12

559

Should I use JSTL ?

Yes.

You can use <c:if> and <c:choose> tags to make conditional rendering in jsp using JSTL.

To simulate if , you can use:

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

To simulate if...else, you can use:

<c:choose>
    <c:when test="${param.enter=='1'}">
        pizza. 
        <br />
    </c:when>    
    <c:otherwise>
        pizzas. 
        <br />
    </c:otherwise>
</c:choose>
Andrea Ligios
  • 46,329
  • 24
  • 102
  • 208
jmj
  • 225,392
  • 41
  • 383
  • 426
  • Thx for that...Actually I am more into UI dev...So do not have much knowledge of JSTL..Could you please provide me a similar working example of JSTL..I mean for if..else – testndtv May 09 '11 at 11:04
  • Thx for the example..The condition I have is actually a JS condition..if navigator.userAgent.match(/iPad/i) != null So can I write that directly in the if test="condition".. – testndtv May 09 '11 at 11:07
  • 1
    well JSTL will execute at server end it will crate HTML out of it and pass it to client – jmj May 09 '11 at 11:19
  • Ok..that's fine...But for the actual condition i.e.in the test="condition" ...Can I specify any JS condition and if yes, can I write it directly e.g. Can I write – testndtv May 09 '11 at 11:20
  • can you update your question with the condition you want to test – jmj May 09 '11 at 11:21
172

If you just want to output different text, a more concise example would be

${condition ? "some text when true" : "some text when false"}

It is way shorter than c:choose.

KIR
  • 5,384
  • 1
  • 26
  • 23
108

The construct for this is:

<c:choose>
   <c:when test="${..}">...</c:when> <!-- if condition -->
   <c:when test="${..}">...</c:when> <!-- else if condition -->
   <c:otherwise>...</c:otherwise>    <!-- else condition -->
</c:choose>

If the condition isn't expensive, I sometimes prefer to simply use two distinct <c:if tags - it makes it easier to read.

Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
12

In case you want to compare strings, write the following JSTL:

<c:choose>
    <c:when test="${myvar.equals('foo')}">
        ...
    </c:when>
    <c:when test="${myvar.equals('bar')}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>
aebersold
  • 9,670
  • 2
  • 18
  • 28
  • I think you can only invoke methods (like .equals) on new(ish) containers, like Tomcat 7+. – Ring Jul 21 '14 at 17:29
11
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<c:set var="val" value="5"/>
<c:choose> 
  <c:when test="${val == '5'}">
    Value is 5
  </c:when>
  <c:otherwise>
    Value is not 5
  </c:otherwise>
</c:choose>
Hiren Odedra
  • 183
  • 1
  • 3
7

simple way :

<c:if test="${condition}">
    //if
</c:if>
<c:if test="${!condition}">
    //else
</c:if>
Milad Heidari
  • 141
  • 1
  • 7
2
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<c:set var="isiPad" value="value"/>
<c:choose>
   <!-- if condition -->
   <c:when test="${...}">Html Code</c:when> 
   <!-- else condition -->
   <c:otherwise>Html code</c:otherwise>   
</c:choose>
aristotll
  • 6,572
  • 5
  • 30
  • 47
2

You can write if-else condition inside <% %> in jsp pages and html code outside of <% %>

For example:

   <%
        String username = (String)session.getAttribute("username");
        if(username==null)  {
    %>            
        <p> username is null</p> //html code
    <%
        } else {
    %>
        <p> username is not null</p> //html code
    <%
        }
    %>
Shivam
  • 63
  • 2
  • 7
1
<c:if test="${any_cond}" var="condition">
    //if part
</c:if>
<c:if test="${!condition}">
    //else part
</c:if>
Sachin Kumar
  • 804
  • 5
  • 11
-1

If you want to do the following by using JSTL Tag Libe, please follow the following steps:

[Requirement] if a number is a grater than equal 40 and lower than 50 then display "Two digit number starting with 4" otherwise "Other numbers".

[Solutions]

1. Please Add the JSTL tag lib on the top of the page.`
     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>`

2. Please Write the following code
`
<c:choose>
       <c:when test="${params.number >=40 && params.number <50}">
              <p> Two digit number starting with 4. </p>
       </c:when>
       <c:otherwise>
              <p> Other numbers. </p>
       </c:otherwise>
  </c:choose>`
-1

In case you want to compare strings, write the following JSTL :

<c:choose>
    <c:when test="${myvar.equals('foo')}">
        ...
    </c:when>
    <c:when test="${myvar.equals('bar')}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>
Codekie
  • 6,585
  • 3
  • 16
  • 24
-1
<c:choose>
<c:when test="${not empty userid and userid ne null}">
      <sql:query dataSource="${dbsource}" var="usersql">
                SELECT * FROM newuser WHERE ID = ?;
                <sql:param value="${param.userid}" />
      </sql:query>
 </c:when>
 <c:otherwise >
       <sql:query dataSource="${dbsource}" var="usersql">
                 SELECT * FROM newuser WHERE username = ?;
                 <sql:param value="${param.username}" />
       </sql:query>                              
  </c:otherwise>