Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting the no of parameter from c:forEach

 
salil verma
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead you can try fn:length to get the number of items
 
salil verma
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic