Share on Google+Share on Google+


IF-ELSE in JSTL section explains you how the if-else tag can be use in web page development.


In this section we will read about implementation of if-else tag in JSTL. We will see the implementation of this tag using a simple example.

In JSTL if-else tag is not implemented directly like the if-else statement is used on the Java/JSP coding. Implementation of if-else tag in JSTL coding is not same as Java/JSP coding. To implement the if-else in JSTL coding there are some tags defined in JSTL these are as follows :

<c:choose> : This tag is like the 'if' notion used in Java/JSP programming. This is a simple tag that is used to perform the conditional operations using the tags <when> and <otherwise>.

<c:when> : Sub tag of <c:choose> tag. It is used to provide a condition to be evaluate. The condition should be evaluated to boolean expression. If this boolean expression evaluates to true then the rest of the body of this tag is evaluated.

<c:otherwise> : Sub tag of <c:choose> tag. It is used with the <c:when> tag. Body of this tag is evaluated when the condition provided to the <c:when> tag is evaluated to 'false'.

Combination of above three tags are used as an if-else statement in JSTL programming. Sample code given below expresses you how this tag is used.:

<c:when test="${condition1}">
<c:when test="${condition2}">


Here an example is being given which will demonstrate you how to use the if-else conditional statement in JSTL. In this example we will create a simple JSP page where we will use the JSTL <c:choose>, <c:when> and <c:otherwise> tag to apply the if-else operation. We will use the Eclipse IDE and Tomcat 7 server for writing and deploying the application. At first we will create a new dynamic web project then we will create a JSP page inside the Web Content folder then we will use the above mentioned tag in the JSP page.

Directory Structure


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@ taglib uri="" prefix="c" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL IF-ELSE Statement</title>
<form action="#">
<p>Enter Name : 
<input type="text" name="name"/></p>
<p>Enter Password : 
<input type="password" name="password"/></p>
<p><input type="submit" value="submit"/></p>
String name = request.getParameter("name");
String password = request.getParameter("password");
if(!(name == null || (name.equals(""))))
<c:set var="nm" value="<%=name %>"/>
<c:set var="psw" value="<%=password %>"/>
<c:when test="${(nm == 'deepak') && (psw == 'deepak')}">
<p>Welcome, <c:out value="${nm }"/></p>
<p>You Are Not A Person, Whom I Am Looking For</p>


When you will deploy the above example you will get the output as follows :

When you will input the value of fields 'name' and 'password' to 'deepak' and 'deepak' then the body of <c:when> tag will be evaluated and the output will be as follows :

When you will input the value of fields 'name' and 'password' other than 'deepak' then the body of <c:otherwise> tag will be evaluated and then the output will be as follows :

Download Source Code


Posted on: August 14, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: IF-ELSE In JSTL  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image