I am getting the below error when I compile the below code
Enumeration e = bean.getSession().getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
System.out.println(name + " = " + value);
Error:
found : java.util.Iterator
requ开发者_如何学Pythonired: java.util.Enumeration
Enumeration e = bean.getSession().getAttributeNames();
You shouldn't be using an enumeration, it should be an Iterator. Then use the methods of the Iterator like hasNext() to check if there is a next item and next() to get the next item. Hope it helps :)
how about just using a for loop?
for (String name : bean.getSession().getAttributeNames() ) {
String value = session.getAttribute(name).toString();
System.out.println( name + " = " + value );
}
It looks like you're actually using a getAttributeNames()
method that return an instance of java.util.Iterator
. So as a quick fix, this should work:
Iterator it = bean.getSession().getAttributeNames();
while (it.hasNext()) {
String name = (String)it.next();
String value = session.getAttribute(name).toString();
System.out.println(name + " = " + value);
More help/info could be provided if we knew the actual types of the bean
variable and/or the return value of bean.getSession()
.
I think the problem is in the first line. This works for me:
<%
Enumeration attrs = session.getAttributeNames(); //you will need to include java.util.Enumeration
while(attrs.hasMoreElements()){ //for each item in the session array
String id = attrs.nextElement().toString(); //the name of the attribute
out.print("'" + id + "': '" + session.getAttribute(id) + "'"); //print out the key/value pair
}
%>
As others pointed out though, you should be using an Iterator
精彩评论