tags:

views:

604

answers:

2

Hi this is a trivial question, but I cannot seem to get it to work.

I am populating a vector from my jsp. (I have verified that the vector has elements in it)

Now, I want to output the contents of the same vector back to my same jsp.

Any takers out there for some code?

A: 
<% 
for (Iterator it = vector.iterator(); it.hasNext();) {
   out.println("Hey here is an item in my vector: " + it.next());
}
%>

?

Hard to do much else without details.

Also, people really still use Vector, and not List?

matt b
I don't post scriptlets anymore, loosely speaking. Its okay if you do. :)
Adeel Ansari
That worked just fine. I appreciate your feedback!
A: 

If you want to avoid scriptlet code, you can do this with the JSTL <c:forEach> tag. For example, to print out each item as a sepate entry in an ordered list, use:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head></head>
  <body>
    <ol>
      <c:forEach items="${vector}" var="item">
        <li><c:out value="${currentName}" /></li>
      </c:forEach>
    </ol>
  </body>
</html>

If you don't care about encoding currentName, you can shorten this to:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
  <head></head>
  <body>
    <ol>
      <c:forEach items="${vector}" var="item">
        <li>${currentName}</li>
      </c:forEach>
    </ol>
  </body>
</html>
Don
I will upvote you if you fix the JSTL taglib URI to use the newer 1.1/1.2 one instead of the >10 years old and deprecated 1.0 ... By the way, the last way isn't specifically "using EL", the first way does that as well. The difference is only that the last way is only possible since Servlet 2.4/JSP 2.0 and that the first way removes XSS risks if `currentName` is actually user-controlled input.
BalusC
Sorry, my answer is non-negotiable......OK, OK, I'll make the changes!
Don