JSTL iterate over list of objects


JSTL iterate over list of objects

JSTL iterate over list of objects is an important aspect for every java developer so let’s understand how to iterate List object in JSTL.

JSTL iterate over list of objects Example

  1. StudentInfo.java as a bean class
  2. StudentHandler.java as a Servet
  3. welcome.jsp to display List objects.

StudentInfo.java

package com.javagf.beans;

public class StudentInfo {
 protected String roll;
 protected String name;
 protected String address;

 public StudentInfo(String roll, String name, String address) {
 this.roll = roll;
 this.name = name;
 this.address = address;
 }

 /**
 * @return the roll
 */
 public String getRoll() {
 return roll;
 }

 /**
 * @param roll
 * the roll to set
 */
 public void setRoll(String roll) {
 this.roll = roll;
 }

 /**
 * @return the name
 */
 public String getName() {
 return name;
 }

 /**
 * @param name
 * the name to set
 */
 public void setName(String name) {
 this.name = name;
 }

 /**
 * @return the address
 */
 public String getAddress() {
 return address;
 }

 /**
 * @param address
 * the address to set
 */
 public void setAddress(String address) {
 this.address = address;
 }

}

StudentHandler.java

package com.javagf.controllers;

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

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.javagf.beans.StudentInfo;

/**
 * Servlet implementation class StudentHandler
 */
@WebServlet("/StudentHandler")
public class StudentHandler extends HttpServlet {
 private static final long serialVersionUID = 1L;

 @Override
 protected void service(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 List<StudentInfo> infos = null;
 infos = new ArrayList<>();
 infos.add(new StudentInfo("101", "Vikas", "Hyderabad"));
 infos.add(new StudentInfo("102", "Amar", "Pune"));
 infos.add(new StudentInfo("103", "Kapil", "Patna"));
 request.setAttribute("stud", infos);
 request.getRequestDispatcher("./welcome.jsp").forward(request, response);

 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 service(request, response);
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 service(request, response);
 }

}

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!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>StudentInfo</title>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
</head>
<body>
 <table>
 <tr>
 <td colspan="3"><h1>Student Information</h1></td>
 </tr>
 <tr>
 <td>Roll</td>
 <td>Name</td>
 <td>Address</td>
 </tr>
 <c:forEach items="${stud}" var="s">
 <tr>
 <td>${s.roll}</td>
 <td>${s.name}</td>
 <td>${s.address}</td>
 </tr>
 </c:forEach>
 </table>
</body>
</html>

Output

Student Information

Roll Name Address
101 Vikas Hyderabad
102 Amar Pune
103 Kapil Patna






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229