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
 */
@WebServlet("/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();
		stu1.setId(001); 
		stu1.setName("Aariz");
		Student stu2 = new Student();
		stu2.setId(002); 
		stu2.setName("Saaim");
		stList.add(stu1);
		stList.add(stu2);
		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"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>main</title>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;
}
table#t01 {
    width: 100%;
    background-color: #f1f1c1;
}
</style></head>
<body>
<%-- Using JSTL core tags forEach and out to loop a list and print items in table --%>
<table id="t01">
<tbody>
<tr><th>ID</th><th>Name</th></tr>
<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>
</tr>
</c:forEach>
</tbody>
</table>
<br><br>
<%-- c:set set variable value --%>
<c:set var="id" value="10" scope="request"></c:set>
<c:out value="${requestScope.id }" ></c:out>
<br><br>
<%-- c:catch print the exception --%>
<c:catch var ="exception">
   <% int x = 15/0;%>
</c:catch>

<c:if test = "${exception ne null}">
   <p>Exception is : ${exception} <br>
   Exception Message: ${exception.message}</p>
</c:if>
<br><br>
<%-- 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>
</body>
</html>
5. Include the below jars to run the example.
Jstl-1.2.jar
Standard-1.1.2.jar
Servlet-api-3.0.jar
6. Run the project using below url

http://localhost:8080/Jstl_CoreTagsExample/MainServlet

Jstl core tags example output

For more details please see
Oracle Java SE Documentation
      Advertisements