JSTL Core Tags

In JSTL, core tags are most frequently used tags that provides variable support, manages URL and control the flow of JSP page. To use JSTL it is required to associate the below URL in your JSP page: -

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

It is not mandatory to use ‘c’ in the prefix of JSTL core tag. You can also use any other character or word. But as a convention, it is better to use it.

Core Tags

Here, is the list of JSTL core tags: -

  • <c:out> tag: - This tag is used to print statements or results. It works as an alternative of <%=expression %> tag.
  • <c:set> tag: - This tag is used to declare the variable with its value and scope.
  • <c:remove> tag: - This tag is used to remove the attributes.
  • <c:if> tag: - This tag works as a conditional statement and used for testing conditions.
  • <c:choose> tag: - The role of this tag is similar to switch statement.
  • <c:when> tag: - The role of this tag is similar to case in switch statement. Thus, it test the conditions.
  • <c:otherwise> tag: - This tag behaves similar to default in switch statement. Thus, it executes when no condition for <c:when tag> matches.
  • <c:catch> tag: - This tag is used to handle the exceptions.
  • <c:import> tag: - This tag includes another files of any type such as JSP, HTML, XML etc. to current JSP file.
  • <c:forEach> tag: - This is an iteration tag used to traverse the elements.
  • <c:param> tag: - This tag is used to add the parameters.
  • <c:url> tag: -

Example of JSTL core Tag

In this example, the functionality of certain JSTL core tags are shown in an easy way.

index.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<html> 
<head> 
<title>Core Tag Example</title> 
</head> 
<body> 
<c:set var="marks" scope="session" value="${35}"/>
<c:if test="${marks>30}">
<p>Congrats!! You <c:out value="${'passed'}"/> the exam </p>
</c:if>
<c:choose>
<c:when test="${marks>80}">
<c:out value="${'Good Performance'}"/>
</c:when>
<c:when test="${marks<80||marks>30}">
<c:out value="${'Average Performance'}"/>
</c:when>
<c:otherwise>
<c:out value="${'Bad Performance'}"/>
</c:otherwise>
</c:choose>
</body> 
</html>
Output: