JSTL core tags provide support for iteration, conditional logic, exception handling, URL manipulation, response forwarding or redirection, and more.
Adding JSTL to Your Project
To use JSTL in your JSP pages, you need to include the JSTL library in your project. You can add the JSTL dependency to your pom.xml
file if you are using Maven. Make sure to use the latest version available:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Alternatively, you can download the JSTL JAR files and add them to your project's WEB-INF/lib
directory.
Importing JSTL Core Library
To use JSTL core tags, include the following directive at the top of your JSP page:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSTL Core Tags List
<c:out>
- Like<%= ... %>
, but for expressions.<c:set>
- Sets the result of an expression evaluation in a 'scope'.<c:remove>
- Removes a scoped variable (from a particular scope, if specified).<c:catch>
- Catches any Throwable that occurs in its body and optionally exposes it.<c:if>
- Simple conditional tag which evaluates its body if the supplied condition is true.<c:choose>
- Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by<c:when>
and<c:otherwise>
.<c:when>
- Subtag of<c:choose>
that includes its body if its condition evaluates to 'true'.<c:otherwise>
- Subtag of<c:choose>
that runs only if all of the prior conditions evaluated to 'false'.<c:import>
- Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.<c:forEach>
- The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality.<c:forTokens>
- Iterates over tokens, separated by the supplied delimiters.<c:param>
- Adds a parameter to a containing 'import' tag's URL.<c:redirect>
- Redirects to a new URL.<c:url>
- Creates a URL with optional query parameters.
How to Use JSTL Core Tags?
To use JSTL core tags, include the following line of code in your JSP page:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Let's discuss important JSTL core tags with examples.
JSTL <c:forEach>
Tag
The <c:forEach>
tag in JSTL is used for executing the same set of statements for a finite number of times. It’s similar to the for
loop in Java. This is a basic iteration tag, accepting many different collection types and supporting subsetting and other functionality.
Syntax:
<c:forEach var="counter_variable_name" begin="initial_value" end="final_limit">
// Block of statements
</c:forEach>
The below are the three main attributes of the <c:forEach>
tag:
- begin: The initial counter value.
- end: The final limit till which the loop will execute.
- var: Counter variable name.
Example:
Let's create a Student
bean class and iterate over a list of students using the <c:forEach>
tag.
Student.java:
package net.javaguides.jstl;
public class Student {
private String firstName;
private String lastName;
private boolean goldCustomer;
public Student(String firstName, String lastName, boolean goldCustomer) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.goldCustomer = goldCustomer;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isGoldCustomer() {
return goldCustomer;
}
public void setGoldCustomer(boolean goldCustomer) {
this.goldCustomer = goldCustomer;
}
}
for-each-student-test.jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*,net.javaguides.jstl.Student" %>
<%
// Just create some sample data ... normally provided by MVC
List<Student> data = new ArrayList<>();
data.add(new Student("Ramesh", "Fadatare", false));
data.add(new Student("John", "Cena", false));
data.add(new Student("Tom", "Cruise", false));
data.add(new Student("Tony", "Stark", false));
data.add(new Student("Prakash", "Jadhav", true));
pageContext.setAttribute("myStudents", data);
%>
<html>
<body>
<h1>List of students</h1>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Gold Customer</th>
</tr>
<c:forEach var="tempStudent" items="${myStudents}">
<tr>
<td>${tempStudent.firstName}</td>
<td>${tempStudent.lastName}</td>
<td>${tempStudent.goldCustomer}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Output:
JSTL <c:choose>
, <c:when>
, <c:otherwise>
Core Tags
These tags are used together like switch-case and default statements in Java. <c:choose>
acts like a switch, <c:when>
like a case (can be used multiple times inside <c:choose>
), and <c:otherwise>
like a default statement.
Syntax:
<c:choose>
<c:when test="${condition1}">
// Do something if condition1 is true
</c:when>
<c:when test="${condition2}">
// Do something if condition2 is true
</c:when>
<c:otherwise>
// Statements executed when all <c:when> tests are false
</c:choose>
Example:
In this example, we will iterate over each student and check whether the student is a gold member or not:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*,net.javaguides.jstl.Student" %>
<%
List<Student> data = new ArrayList<>();
data.add(new Student("Ramesh", "Fadatare", false));
data.add(new Student("John", "Cena", false));
data.add(new Student("Tom", "Cruise", false));
data.add(new Student("Tony", "Stark", false));
data.add(new Student("Prakash", "Jadhav", true));
pageContext.setAttribute("myStudents", data);
%>
<html>
<body>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Gold Customer</th>
</tr>
<c:forEach var="tempStudent" items="${myStudents}">
<tr>
<td>${tempStudent.firstName}</td>
<td>${tempStudent.lastName}</td>
<td>
<c:choose>
<c:when test="${tempStudent.goldCustomer}">
Special Discount
</c:when>
<c:otherwise>
no soup for you!
</c:otherwise
>
</c:choose>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Output:
JSTL <c:if>
Core Tag
The <c:if>
tag is a JSTL core tag used for testing conditions. It is similar to an if
statement in Java, which evaluates a condition and executes a block of code if the result is true
.
Syntax:
<c:if test="${condition}">
...
</c:if>
Example:
In this example, we will test if the student is a gold member or not using the <c:if>
tag:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*,net.javaguides.jstl.Student" %>
<%
List<Student> data = new ArrayList<>();
data.add(new Student("Ramesh", "Fadatare", false));
data.add(new Student("John", "Cena", false));
data.add(new Student("Tom", "Cruise", false));
data.add(new Student("Tony", "Stark", false));
data.add(new Student("Prakash", "Jadhav", true));
pageContext.setAttribute("myStudents", data);
%>
<html>
<body>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Gold Customer</th>
</tr>
<c:forEach var="tempStudent" items="${myStudents}">
<tr>
<td>${tempStudent.firstName}</td>
<td>${tempStudent.lastName}</td>
<td>
<c:if test="${tempStudent.goldCustomer}">
Special Discount
</c:if>
<c:if test="${not tempStudent.goldCustomer}">
-
</c:if>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Output:
Notice that the output here iterates over each student and checks the condition for a special discount, printing the appropriate message against each student.
By leveraging these JSTL core tags, you can create more dynamic and maintainable JSP pages.
Comments
Post a Comment
Leave Comment