if...else within JSP or JSTL

JspIf StatementJstl

Jsp Problem Overview


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?

Jsp Solutions


Solution 1 - Jsp

>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>

Solution 2 - Jsp

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.

Solution 3 - Jsp

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.

Solution 4 - Jsp

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>

Solution 5 - Jsp

<%@ 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>

Solution 6 - Jsp

simple way :

<c:if test="${condition}">
    //if
</c:if>
<c:if test="${!condition}">
    //else
</c:if>

Solution 7 - Jsp

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
    <%
   		}
    %>

Solution 8 - Jsp

<%@ 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>

Solution 9 - Jsp

<c:if test="${any_cond}" var="condition">
    //if part
</c:if>
<c:if test="${!condition}">
    //else part
</c:if>

Solution 10 - Jsp

<c:if test="${condition1 && condition2 && condition3}" var="condition">
    //if
</c:if>
<c:if test="${!condition}">
    //else
</c:if>

This is an another way to take the if condition in a variable.

Solution 11 - Jsp

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>`

Solution 12 - Jsp

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>

Solution 13 - Jsp

<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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncopenndthagenView Question on Stackoverflow
Solution 1 - JspjmjView Answer on Stackoverflow
Solution 2 - JspKIRView Answer on Stackoverflow
Solution 3 - JspBozhoView Answer on Stackoverflow
Solution 4 - JspaebersoldView Answer on Stackoverflow
Solution 5 - JspHiren OdedraView Answer on Stackoverflow
Solution 6 - JspMilad HeidariView Answer on Stackoverflow
Solution 7 - JspShivamView Answer on Stackoverflow
Solution 8 - JspBhushan SanghaviView Answer on Stackoverflow
Solution 9 - JspSachin KumarView Answer on Stackoverflow
Solution 10 - JspSachin KumarView Answer on Stackoverflow
Solution 11 - Jspuser3466950View Answer on Stackoverflow
Solution 12 - JspMohammad SherafatianView Answer on Stackoverflow
Solution 13 - JspAman KumarView Answer on Stackoverflow