Home Jsp Simple-jsp-example JSTL: another for each and status
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JSTL: another for each and status

Advertisement
In this program we are declaring one array of type String in which we are going to store some movies name in it. This declaration will be done inside the scriptlet directive. Now set the attribute by using the pageContext implicit object.

JSTL: another for each and status

        

In this program we are declaring one array of type String in which we are going to store some movies name in it. This declaration will be done inside the scriptlet directive. Now set the attribute by using the pageContext implicit object. 

To retrieve the values set in the directive we now use <c: forEach> tag which will help us to iterate over the values. In this tag we have used some attributes like var which will help us to iterate the values i.e. movies. The attribute items helps us to get the attribute which we have set in the directive. The varStatus attribute makes a new variable that holds an instance of javax.servlet.jsp.jstl.core.LoopTagStatus.  Use <c:out> to display the result to the browser.

The forEach has one scoped variable called count. The LoopTagStatus class has a count property which gives us the current value of the iteration counter.

The code of the program is given below:

 

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

        <%

    String[] movies = {"Sholay", "Maine Pyar kiya", "Jai Santoshi Maa", "Kaho Na Pyaar Hai", "Krish", "Rang De Basanti"};

    pageContext.setAttribute("movies", movies, pageContext.PAGE_SCOPE);

        %>

        <html>

  <head>

    <title>forEach and status</title>

  </head>

  <body>

    The forEach has one scoped variable called 'count', it tells the 

	current position of the element within the collection. <br>

    <c:forEach var="movieName" items="${pageScope.movies}" varStatus="status">

      Movie Number :  <c:out value="${status.count}" /> is

        <c:out value="${movieName}" /> <br />

    </c:forEach>

  </body>

        </html>

The output of the program is given below:

Download this example.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 12, 2008

Ask Questions?    Discuss: JSTL: another for each and status  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com