Menu

If else || nested if statement in JSTL

Today we will discuss about; how to write simple if else and nested if else statements in JSTL.

If you have to check only a single value then you can use if statement "<c:if>" in JSTL. But if you want to achieve nested if else then you have to use <c:when> and <c:otherwise>.

Will will see both if and nested if else in JSTL with an example.

To use the JSTL in your JSP file we have to first import the JSTL tag library in our JSP file. Below is the import statement to where we are importing JSTL core tag library in our JSP. Specify a prefix to this tag which we will prefix further with our tag.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
prefix="c" use c when you use JSTL tags in your JSP file.
e.g. <c:if>, <c:choose>

Suppose we have a variable name which have a name of an user. let see how will check this using if statement in jstl.
Using <c:set> tag we can declare and set value to a variable. In below statement we are declaring a variable name and assigning its value RASHID.
<c:set var=name value="RASHID">
Now see the if condition 
 <c:if test="${name == 'RASHID'}">
 <c:set var=age value=23 />
</c:if>
Now we will using nested if else how will check the value of variable name in jstl.
<c:choose>
         <c:when test = "${name == 'RASHID'}">
            HIS NAME IS RASHID.
         </c:when>
         <c:when test = "${name == 'Ranjan'}">
            HIS NAME IS Ranjan.
         </c:when>
         <c:otherwise>
            No machtes....
         </c:otherwise>
 </c:choose>
Hope this will help you to understand the if else and nested if else condition using JSTL. Do let me know if you have any question and suggestions. 

Filmy Quiz

Question: What lasagna-loving cat starred in his own feature film in 2004? 
Correct Answer: Garfield

Question: Zootopia's' Nick Wilde is modeled after what other Disney fox? 
Correct Answer:Robin Hood
Question: What is the name of the titular dog that starred in this 2008 film with Owen Wilson? 
Correct Answer:Marley

Question: What 2003 film is a true story about an undersized Depression-era racehorse? 
Correct Answer:Seabiscuit

Question: In 1773, The Resolution became the first ship to accomplish what feat? 
Correct Answer:Cross Antarctica
Question: What year did the Titanic set sail? 
Correct Answer:1912

Question: Kite capital of the world, where Kites were first created? 
Correct Answer:Weifang, Shandong, China

If else condition in shell script with $? and grep command

In shell script condition check is very easy and simple using if else statements. Here is a example which we would like to discuss with our readers.

Scenario: We have a variable that contains long string value, user wants to find matching value from given string and perform check whether that text/character has matched from string value or or not.


#Program in Bash Shell
Line 1: CHECK_SOURCE="[main] INFO com.adobe.jcr.checknode.existence - Node does not exist"
line 2: echo $CHECK_SOURCE | grep -w -o -i 'Node exists'
Line 3: if [ $? == 0 ]; then
Line 4:        echo " matched"
Line 5: else 
Line 6: echo "not matched" 
Line 7: fi 

Explanation:

In line 1 we have declared a variable CHECK_SOURCE which have some value.
In line 2 we are finding text "Node exists" in value of variable CHECK_SOURCE using grep command.
In line 3 using if statement we are checking number of matches from last executed statement. $? sign will return number of matches. If text passed in grep command will find some matching text in variable then $? will number of matching otherwise it will return zero.