BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

JSTL <c:if> Core Tag

Last Updated: November 29, 2013 by Chaitanya Singh | Filed Under: JSTL

<c:if> is a JSTL core tag which is used for testing conditions. It is more or like a if statement in java which evaluates a condition and executes a block of code if the result is true.

Syntax:

This is the basic syntax of <c:if> core tag. The set of statements enclosed within <c:if> tag gets executed if test=”true”. For using this tag we generally use expression language to evaluate an relational expression. We use EL because it returns boolean value(true/false) after evaluating the condition and we need the boolean value for test attribute.

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

Example of <c:if> tag

In this example we have defined age variable using <c:set> tag and then we are checking the eligibility of voting by using <c:if> tag.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL c:if Tag Example</title>
</head>
<body>
<c:set var="age" value="26"/>
<c:if test="${age >= 18}">
 <c:out value="You are eligible for voting!"/>
</c:if>
<c:if test="${age < 18}">
 <c:out value="You are not eligible for voting!"/>
</c:if>
</body>
</html>

Output:

c-if-example

<c:if> attributes

Above we have seen the basic usage of <c:if> where we have used only the test attribute. However there are two other optional attributes for this tag which are var and scope. Using these attributes you can simply store the test results in a variable within a specified scope.

  • var: Variable name in which the test result would be stored.
  • scope: It defines the scope for storing the value. For e.g. if its session the stored var value can be accessed till the session is active.

An example of var and scope attribute

Storing the test result in variable res in request scope. For printing the value we have given requestScope.res as the variable is stored in request however you can even give variable name(res) alone, it would work fine.

<c:if test="${17 >= 18}" var="res" scope="request">
</c:if>
<c:out value="${requestScope.res}"/>

c-if-example2

Top Related Articles:

  1. JSTL <c:set> Core Tag
  2. JSTL <c:redirect> Core Tag
  3. JSTL <c:out> Core Tag
  4. JSTL <c:choose>, <c:when>, <c:otherwise> Core Tags
  5. JSTL <c:url> Core Tag

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. sahul says

    September 10, 2014 at 1:21 PM

    This is really very good site for learning jstl for beginners

    Reply
  2. keyur says

    November 24, 2015 at 1:38 PM

    good site for learning jstl..

    Reply
  3. Nilesh Dhande says

    February 26, 2016 at 6:33 AM

    Really, this is very useful explanation and thanks…

    Reply
  4. seema says

    April 6, 2016 at 9:02 AM

    good site for learning jstl

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSTL Core Tags

  • c:out
  • c:set
  • c:remove
  • c:if
  • c:choose
  • c:when
  • c:otherwise
  • c:catch
  • c:import
  • c:forEach
  • c:forTokens
  • c:param
  • c:url
  • c:redirect
JSTL Functions
  • fn:contains()
  • fn:containsIgnoreCase()
  • fn:indexOf()
  • fn:escapeXML()
  • fn:join() & fn:split()
  • fn:length()
  • fn:startsWith()
  • fn:endsWith()
  • fn:substring()
  • fn:substringAfter()
  • fn:substringBefore()
  • fn:trim()
  • fn:toUpperCase()
  • fn:toLowerCase()
  • fn:replace()

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap