• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

getting the no of parameter from c:forEach

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to jstl
While trying the following code,i am gettting the items in the string printed but not the no of parameters in the array

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Main extends HttpServlet implements Servlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] names={"salil verma","amit tripathi","amit pandey","amit singh"};
request.setAttribute("names",names);
RequestDispatcher rd = request.getRequestDispatcher("Main.jsp");
rd.forward(request,response);
}
}



The code for Main.jsp is as follows-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<HTML>
<HEAD>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM Software Development Platform">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE>Main.jsp</TITLE>
</HEAD>
<BODY>
<p> The attributes in the request are as follows <br>
<c:forEach var="name" items="${names}" varStatus="Count">
<br> ${name}
</c:forEach>
<p> The no of elements in the string array are = ${Count.count}
</BODY>
</HTML>

I get the following as output--

The attributes in the request are as follows

salil verma
amit tripathi
amit pandey
amit singh

The no of elements in the string array are =


But the no of elements are not printed. So can any one help me
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
varStatus only exists within the forEach. You are referencing it after the forEach where it has become out of scope.

If you move the ${Count.count} to inside the forEach you should see it work. Over and over again.
[ June 20, 2006: Message edited by: Marc Peabody ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead you can try fn:length to get the number of items
 
salil verma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc, thanks for reply. got the mistake that i had done.

Satou,thanks for your reply too. Can you send the whole syntax to me as i am unable understand the syntax.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic