JSTL Core Tags Example

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Here are the steps to use and run the JSTL core tags.

1. Create the dynamic web project JSTLCoreTagsExample.

Jstl core tags example

2. Create MainServlet class.
package com.javatutsworld.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javatutsworld.pojo.Student;

 * Servlet implementation class MainServlet
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

     * Default constructor. 
    public MainServlet() {
        // TODO Auto-generated constructor stub

	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Student> stList = new ArrayList<Student>();
		Student stu1 = new Student();
		Student stu2 = new Student();
		request.setAttribute("stList", stList);
		request.setAttribute("url", "http://www.javatutsworld.com");
		RequestDispatcher rd = getServletContext().getRequestDispatcher("/main.jsp");
		rd.forward(request, response);


	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

3. Create the Student pojo class.
package com.javatutsworld.pojo;

public class Student {
		private int id;
		public Student(){
		public int getId() {
			return id;
		public void setId(int id) {
			this.id = id;
		public String getName() {
			return name;
		public void setName(String name) {
			this.name = name;
		private String name;
4. Create the main.jsp file.

<%@ page language="java" contentType="text/html; charset=US-ASCII"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
th, td {
    padding: 5px;
    text-align: left;
table#t01 {
    width: 100%;
    background-color: #f1f1c1;
<%-- Using JSTL core tags forEach and out to loop a list and print items in table --%>
<table id="t01">
<c:forEach items="${requestScope.stList}" var="stu">
<tr><td><c:out value="${stu.id}"></c:out></td>
<td><c:out value="${stu.name}"></c:out></td>
<%-- c:set set variable value --%>
<c:set var="id" value="10" scope="request"></c:set>
<c:out value="${requestScope.id }" ></c:out>
<%-- c:catch print the exception --%>
<c:catch var ="exception">
   <% int x = 15/0;%>

<c:if test = "${exception ne null}">
   <p>Exception is : ${exception} <br>
   Exception Message: ${exception.message}</p>
<%-- c:url show how to get the url value which is set in the servlet --%>
<a href="<c:url value="${requestScope.url }"></c:url>">JavaTutsWorld</a>
5. Include the below jars to run the example.
6. Run the project using below url


Jstl core tags example output

For more details please see
Oracle Java SE Documentation