1-JSTL Check Equals, not equals

JSTL Check Equals(==), not equals(!=) explains about how to use logical conditions(equals, not equals etc) with JSTL tag
Consider a JSP Page where you need to check a value whether it is equals or not equals and process accordingly, in that scenario,  you can follow this example.
On the following table, I am showing 2 different ways (Method 1 & Method 2), you can achieve equals & not equals in JSTL 
Logical operationJSTL Method 1JSTL Method 2
EqualsEq==
Not EqualsNe!=
Required Libraries
You need to download
  1. Tomcat 7
  2. JSTL 1.2
Following jar must be in classpath
  1. jstl-1.2.jar
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<title>Check equals(==), not equals(!=)</title>
</head>
<body>

 <%
    // you can also set the values into request scope same as using c:set
    request.setAttribute("count", 5);
 %>
 <!-- you can check "equals(==)" two ways -->
 <c:if test="${count == 5}">
  count equals to 5<br />
 </c:if>
 
 <!-- you can check "not equals(!=)" two ways -->
 <c:if test="${count != 4}">
     count is not equals 4<br />
 </c:if>
</body>
</html>

2- JSTL c:forEach, c:set

  • Với  ${farmCodeZonesLabelValueMap} = Map<String, Map<String,String>> =>
    duyệt qua mỗi key (String) và value (Map<String,String>)
    tạo một select có id="f" + key
    các option có name/value là key/value của value (Map<String,String>) nói trên.

<div id="bufferSpace"style="display: none;">
<c:forEach var="zonesMap1" items="${farmCodeZonesLabelValueMap}">
<c:set var="name" value="${zonesMap1.value}" />
<select id="f${zonesMap1.key}" >
<option value="NONE">--- Select ---</option>
<c:forEach var="zonesMap11" items="${name}">
<option value="${zonesMap11.key}">${zonesMap11.value}</option>
</c:forEach>
</select>
</c:forEach>
</div>
  • Duyệt các phần tử của một List
 <c:forEach items="${treeFarmModelSearchCriteria.zoneList}" var="zoneForm" varStatus="theCount">
<c:set var="status" value="" />
<c:choose>
 <c:when test="${theCount.count == 1}">
      <c:set var="status" value="active" />
 </c:when>
 <c:otherwise>
     <c:set var="status" value="" />
  </c:otherwise>
</c:choose>
<li class='${status}'> <a data-toggle='tab' href='#izone${zoneForm.zoneId}'><span class='hidden-mobile hidden-tablet'></span><c:out value="${zoneForm.zoneName}"/></a></li>
</c:forEach>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<%
    ArrayList<String> numList = new ArrayList<String>();
    numList.add("one");
    numList.add("two");
    numList.add("three");
    request.setAttribute("numList", numList);
%>
<html>
<body>
    <c:forEach items="${numList}" var="item" varStatus="status">
        ${status.count}) ${item}<br/>
    </c:forEach>
</body> 
</html>
=>
Output
1) one

2) two

3) three
<c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">
    <c:out value="${i}" /> 
</c:forEach>
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Iterate HashMap With JSTL 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.HashMap"%>
<%
    HashMap<String,String> numMap = new HashMap<String,String>();
    numMap.put("1","one");
    numMap.put("2","two");
    numMap.put("3","three");
    request.setAttribute("numMap", numMap);
%>
<html>
<body>
    <c:forEach items="${numMap}" var="entry">
        ${entry.key})${entry.value}<br/>
    </c:forEach>
</body>
</html>
Output
1) one
2) two
3) three

3- c:url & c:param tag

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSTL Example With</title>
</head>
<body>
<div>
<c:url value="/index.jsp" context="/JstlJavaScriptTest" var="url" scope="request">
<c:param name="username" value="rockey" ></c:param>
<c:param name="password" value="password123" ></c:param>
</c:url>
${requestScope.url}

</div>
<div>
<c:url var="url" value="/index.jsp" context="/JSTLExample" scope="session"/>
${sessionScope.url}
</div>
</body>
</html>

=>
Kết quả:
/JstlJavaScriptTest/index.jsp?username=rockey&password=password123
/JSTLExample/index.jsp

4-JSTL forTokens Example

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
    <head>
        <title> JSTL c:forTokens Example</title>
    </head>
    <body>
  
        <!-- JSTL forTokens tag for iterating over comma separated Strings -->
        <c:forTokens var="token" items="Struts, Spring, Hibernate, EJB" delims=",">
   <c:out value="${token}"/> </br>
        </c:forTokens>

 <!-- JSTL forTokens tag for iterating over comma separated Numbers -->
        <c:forTokens delims="," items="10,15,20,25" var="number">
   <c:out value="${number}" default="25"></c:out>
 </c:forTokens>

 <!-- JSTL forTokens tag looping numbers from 1 to 10 with step -->
 <c:forTokens var="item" begin="0" end="10" step="2">
   <c:out value="${item}"/><p>
 </c:forTokens>

    </body>
</html>
Output
Struts
Spring
Hibernate
EJB

10
15
20
25

2
4
6
8
10

5- fn:length() Example

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page import="java.util.ArrayList"%>
<%
    ArrayList<String> numList = new ArrayList<String>();
    numList.add("one");
    numList.add("two");
    numList.add("three");
    request.setAttribute("numList", numList);
%>
<html>
<body>
    The length of the numList: ${fn:length(numList)}<br/>
    The length of the test String: ${fn:length('hello')}
</body>
</html>
Output
The length of the numList: 3
The length of the test String: 5