Updated March 31, 2023
Introduction to JSP if else
JSP if else loop is a conditional statement that enables a code snippet to be executed if a particular condition stands true. This conditional statement works similarly to the if else conditional statements in traditional coding languages like C/C++/Java and many more. This is a powerful impact and is commonly used in programming languages. Programmers can provide more than one condition using this loop with the help of “else-if” functionality.
Syntax of JSP if else
The syntax for JSP if-else loop is given below:
<%
if(condition)
{
Code or set of executable statements that should be processed if conditions stand true.
}
Else if() //optional
{
Code or set of executable statements that should be processed if second condition is true while the first one stands false.
}
Else //optional
{
This condition should be executed if all conditions stand false.
}
%>
The if-else statement should be present on the JSP page. JSP page should be present in a specific directory called “WEB-INF”.
Below is the screenshot of JSP pages which we created as an example “newFile.jsp”, “newFile2.jsp” and they are placed in WEB-INF directory.
How does if else Statement work?
If else statement reads True or False. The conclusion of “True or False” or “1 or 0” is reached when the condition is being evaluated against the given parameters.
For example, the requirement is that the counter should stop when it reaches the number 10.
So a condition can be written in if() function that states:
Code:
if(count<11)
{
count ++; //Assuming that count was initialized with 1.
}
Here, the condition will be true for the first 10 transactions but will turn false once the count variable reached 11.
Examples of JSP if else
Given below are the examples.
Example #1
NewFile.jsp
Code:
<%! int day = 3; %>
<html>
<head>
<title>JSP IF-ELSE</title>
</head>
<body>
<p>This is an JSP IF-ELSE Example in which today's day is compared with Sunday and according the output
screen will show the result. Here, prerequisite is that today's date should be in sync with system date.</p>
<% if (day == 1) { %>
<p> yaaay! Today is Sunday.. </p>
<% } else { %>
<p> Gosh! Today is a Work-day.. </p>
<% } %>
</body>
</html>
Output:
Explanation:
- In this example, a variable day is used to find out if today’s day is Sunday or not. Considering Sunday as 1st day of the week, the control loop is created. The condition passed here is if the day is equal to one or otherwise.
- Previously we designed day as “3” so when the “If” statement compares day value (i.e. 3) with the condition which wants it to be “1” then it returns “FALSE” or “0”. Remember that false is denoted by “0” by computers. Hence, here the false is obtained so control of the program will skip the whole code snippet under if section and move to the “Else” section. “Else” section contains a print statement that is being printed in the output screen too.
Example #2
Code:
<html>
<head>
<title>
JSP IF ELSE- 2
</title>
</head>
<body>
<p>This program takes two input stings and compares those strings. The result is displayed on the screen on the basis of
the strings are not matching, strings are matching but cases are not or matching.</p> <br><br>
<%
String sVariable1 = "JSP";
String sVariable2 = "jsp";
//The below function compares strings stored in two different string variables.
if(sVariable1.equals(sVariable2)) {
%>
The values of string 1 and string 2 are equal.
<br/>
<%
} else if(sVariable1.equalsIgnoreCase(sVariable2)) {
%>
The values of string 1 and string 2 are same but with the difference in case letters.
<br/>
<%
} else {
%>
The values of string 1 and string 2 are not at all equal.<br/>
<%
}
%>
</body>
</html>
Output:
Explanation:
- In this example, two string variables named “sVariable1” and “sVariable2” are declared with a value of “JSP” and “jsp” respectively. Variable one is assigned with the upper case while string 2 is assigned with a lower case. These two strings are compared with the help of different string comparison functions. These string comparison functions are predefined in JSP libraries so we need not worry about the definition of these functions. These functions are used under the “If” and “Else if” sections.
- The first “If” section compared string 1 with string 2 using equals() function. This function compares strings and considers case mismatch also. So, it returns false as the cases are not matching in the strings we provided in this function.
- Second is “Else If” section which contains equalsIgnoreCase() function. This function compares the string and does not consider cases while comparing. So, here function returns true. The eagles the code under “Else-If” section to be processed. The print statement written under “Else-if” section is printed.
- The last section named “Else” is skipped because as soon as we get one condition satisfied then control exits the loop.
Conclusion
JSP if else is a control statement to get the code executed on the basis of results derived conditional statement defined. This is very much used not only in JSP programming but also in other programming languages like Java, C, C++, python, and many. It is very common to see these statements in any existing code. Its use will reduce the work by skipping the whole code piece wherever it is not required.
Recommended Articles
This is a guide to JSP if else. Here we discuss the introduction, how does the if else statement work? along with examples respectively. You may also have a look at the following articles to learn more –