Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default Iterating over an array of objects in jstl

    HI,
    I am new to JSTL. Other day, I came across the <c:forEach/> tag.Now if I have to iterate over an arraylist which contains objects created by the programmer, how do I do that?Suppose the arraylist conatains the objects of the follwing class:-
    class Demo {
    private String name;
    public void setName(String name)
    {
    .....
    }

    public String getName(){......}

    }

    Now suppose I want to display all the names in my JSP page which are contained in the arraylist.Could any one show me a sample code?
    Thanks in advance
    Last edited by rahullahiri007; 07-03-2010 at 05:40 PM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Talking Hey

    Hi Rahul,

    Here goes your code:-


    Demo.java
    Java Code:
    package com.demo;
    
    public class Demo {
    	private String name;
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    }
    TestDemoServlet.java
    Java Code:
    package com.demo;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class TestDemoServlet extends HttpServlet {
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		ArrayList demoNames = new ArrayList();
    
    		Demo d1 = new Demo();
    		d1.setName("Rahul");
    
    		Demo d2 = new Demo();
    		d2.setName("Rahul123");
    
    		Demo d3 = new Demo();
    		d3.setName("Rahul456");
    
    		demoNames.add(d1);
    		demoNames.add(d2);
    		demoNames.add(d3);
    		request.setAttribute("demoNames", demoNames);
    
    		RequestDispatcher view = request.getRequestDispatcher("DemoJSP.jsp");
    		view.forward(request, response);
    	}
    }
    DemoJSP.jsp
    Java Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <html>
    <body>
    <b>The Demo Object Names Are:-
    <br>
    
    <table>
    <c:forEach items="${demoNames}" var="DemoNames">
    	<tr>
    		<td>${DemoNames.name}</td>
    	</tr>
    </c:forEach>
    </table>
    
    </body>
    </html>
    Make sure to have proper package structure as per the first line in java files.

    Configure and then RUN the servlet, that will give you the expected output.

    Hope that helps,

    Goldest

Similar Threads

  1. Displaying array values in jstl page
    By keshaba in forum Advanced Java
    Replies: 0
    Last Post: 04-23-2010, 11:00 AM
  2. Iterating array in jrxml
    By prasad.th16 in forum Advanced Java
    Replies: 1
    Last Post: 10-01-2009, 01:36 PM
  3. iterating through a collection of objects
    By Scotty Boy in forum New To Java
    Replies: 0
    Last Post: 04-10-2008, 02:28 AM
  4. Iterating through result set in JSTL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 04:13 PM
  5. Iterating through result set in JSTL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 10:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •