Wednesday, July 14, 2021

5 JSTL Core IF Tag Examples in JSP - Tutorial

<c:if>  or if the tag of JSTL core tag library in JSP is one of the most versatile and useful tags. JSTL if tag allows you
to test for a condition, like checking for a particular parameter in requestScope, sessionScope, or pageScope. You can also check any parameter in request parameters and headers or can check for a variable in JSP page using <c:if> tag. JSTL if tag helps a lot to reduce the amount of Java code from JSP  page and if used, along with expression language JSTL core tag library, can remove almost all Java code from JSP files. Earlier we have seen examples of JSTL foreach tag and JSTL core set tag and this JSP JSTL tutorial is based on if the tag of the JSTL core tag library.

We will see how to use <core:if> tag inside JSP files and different example of <core:if> tag to get ourselves familiar with the functionality and power offered by JSTL <c:if> tag. After seeing these examples of <core:if> tag along with expression language, You will be amazed to see, how clean your JSP looks like.

What is <core:if> tag in JSTL and JSP

How to use JSTL core if tag in JSP with examples tutorial<core:if> or <c:if> tag, based on prefix you choose on taglib declaration, is part of standard JSTL (Java Standard Tag libary). JSTL <core:if> tag test one condition and display or evaluate body content only if condition is true. If test expression results in false than content of <core:if> body is not evaluated. Here is syntax of JSTL if tag:

<core:if test="boolean expression" var="variable" scope="scope">
body content only be evaluated if test condition will be true

test attribute contains a boolean expression which is used to test a condition while attribute var and scope can be used to store the result of the test into a particular variable specified by var and in a particular scope specified by scope. We will see an example of JSTL if tag for storing test results in the example section.

How to use JSTL IF tag in JSP

Here is a step by step guide to using JSTL <core:if> the tag inside JSP file:

1) Download Java standard tag library or if you are using Maven along with Eclipse or Netbeans IDE then just import
dependency. Netbeans itself maintains a set of libraries including JSTL, Spring, and Struts if you download their web package.

2) Include jstl.jar in the application’s classpath. In a web application, you should put the JAR files on WEB-INF/lib folder, that folder is available on the web application’s classpath.

3) Import the JSTL core tag library in your JSP file by using <taglib> tag

<%@ taglib prefix="core" uri="" %>

Now you are ready to use JSTL if the tag in your JSP  file, just follow the syntax of <c:if> tag and you will be fine. Here prefix core will be used along with IF tag e.g. <core:if>.

JSTL if tag Examples in JSP

Here are some frequently used examples of <c:if> tag for checking request parameters, header values in JSP pages. These examples can be best used to remember the syntax of JSTL if tag.

1) If condition with request parameter in JSP

<core:if test="${ != null}">
How to get: <%=request.getParameter("loan")%>

This block of code in JSP will only execute if the request contains a parameter called loan and will not display anything if the request parameter doesn't contain any parameter named loan.

2) If condition with header parameter in JSP

This is similar to above example of JSTL if tag. Only difference is, instead of request parameters now <core:if> will check headers for a particular parameter as shown in below code:

<core:if test="${header.cookie == null}">
Hello: ${header.cookie}

This JSTL example checks if the cookie is present in the HTTP header and display value of cookie if present.

3) How to check a particular variable in pageScope, requestScope, and sessionScope in JSTL if tag.

In this JSTL If tag example, we have put a variable loan in request scope using <c:set> tag, which is another JSTL tag. Later we check for same variable using <c:if> tag.

<c:set var="loan" value="Property Loan" scope="request" />
<c:if test="${ != null }">
Loan type is ${}

4) How to check for a bean value in JSP if tag

This if tag will only be executed if type property of loan object is "Personal Loan". This way you can check for any property of bean for particular value or null.

<c:if test="${loan.type == 'Personal Loan'}">

5) How to show the result of <c:if> tag test in JSP

You can export result of <c:if> tag condition check using var and scope. var is used to define name of variable which holds result of if tag evaluation and scope define the scope of that variable. In the following example, variable cookieTestResult will hold the result of if tag evaluation in request scope. You can also see these Servlet and JSP courses to learn more about JSTL tags. 

<c:if test="${header.cookie == null}" var="cookieTestResult" scope="request">
Hello: ${header.cookie}

Result of Cookie Test:  <c:out value="${requestScope.cookieTestResult}"/>

Important points on JSTL <core:if> tag

Now we know How to use JSTL <c:if> tag, let’ revise some important things about this tag in JSP.

1. While using if tag from the JSTL core library along with expression language it's good to recap implicit variables available to Expression Language or EL in JSP. They are pageContext, pageScope, requestScope, sessionScope and applcationScope.

They are self-explanatory and represent Map holding request, session, or application-level attributes key is the name of attribute and value form Map is the value of that attribute. 

Apart from these 5, we have 6 more maps called param, paramValues, header, headerValues, cookie, and initParam. As the name suggests they hold request and header parameters, name and values of cookie and init parameters.

2.  Also remember about dot(.) and bracket([]) operator in JSP expression language; dot is used to get property from object and bracket is used to get elements from collections and arrays.

That’s all on How to use <c:if> or JSTL If tag in JSP pages. For Java programmers, who sometimes have added responsibility to code JSP as well, it’s difficult to remember syntax and options of various JSTL tags. These <c:if> tag examples will help to quickly remember how to use JSTL if tag with full of its potential.


Anonymous said...

Best examples and information on JSTL Core IF tag, I have seen in the web. I didn't know that var and scope variables are used for storing result and there scope. Thanks for that. I also like to share one example of JSTL IF tag, which I used to test whether a particular variable is empty or not e.g. if you have a employeeId in request scope, then you can write something like :

<c:if test="${! empty requestScope.empId}">
<a href="detailedInfo.htm?empId=${requestScope.empId}">Employee Details</a>

Anonymous said...

How to use if condition to check the path of the page

javin paul said...

can you elaborate? Any example

Post a Comment