Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从会话获取会话变量_Java_Enumeration - Fatal编程技术网

Java 从会话获取会话变量

Java 从会话获取会话变量,java,enumeration,Java,Enumeration,当我编译下面的代码时,我得到了下面的错误 Enumeration e = bean.getSession().getAttributeNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); String value = session.getAttribute(name).toString(); System.out.prin

当我编译下面的代码时,我得到了下面的错误

Enumeration e = bean.getSession().getAttributeNames(); 
        while (e.hasMoreElements()) { 
        String name = (String)e.nextElement(); 
        String value = session.getAttribute(name).toString(); 
        System.out.println(name + " = " + value); 


您不应该使用枚举,它应该是迭代器。然后使用诸如hasNext()之类的迭代器方法检查是否存在下一项,并使用next()获取下一项。希望有帮助:)

您不应该使用枚举,它应该是迭代器。然后使用诸如hasNext()之类的迭代器方法检查是否存在下一项,并使用next()获取下一项。希望有帮助:)

使用for循环怎么样

for (String name : bean.getSession().getAttributeNames() ) {
    String value = session.getAttribute(name).toString();
    System.out.println( name + " = " + value );
}

使用for循环怎么样

for (String name : bean.getSession().getAttributeNames() ) {
    String value = session.getAttribute(name).toString();
    System.out.println( name + " = " + value );
}

看起来您实际使用的是一个
getAttributeNames()
方法,该方法返回
java.util.Iterator
的实例。因此,作为一个快速解决方案,这应该是可行的:

Iterator it = bean.getSession().getAttributeNames(); 
while (it.hasNext()) { 
    String name = (String)it.next(); 
    String value = session.getAttribute(name).toString(); 
    System.out.println(name + " = " + value);

如果我们知道
bean
变量的实际类型和/或
bean.getSession()

的返回值,就可以提供更多的帮助/信息。看起来您实际使用的是一个
getAttributeNames()
方法,该方法返回
java.util.Iterator
的实例。因此,作为一个快速解决方案,这应该是可行的:

Iterator it = bean.getSession().getAttributeNames(); 
while (it.hasNext()) { 
    String name = (String)it.next(); 
    String value = session.getAttribute(name).toString(); 
    System.out.println(name + " = " + value);

如果我们知道
bean
变量的实际类型和/或
bean.getSession()的返回值,我们可以提供更多的帮助/信息。我想问题在第一行。这对我很有用:

<%
    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
    }
%>


正如其他人指出的,您应该使用迭代器,我认为问题在第一行。这对我很有用:

<%
    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
    }
%>


正如其他人指出的,您应该使用迭代器什么是
bean
getSession()
返回什么?当您应该使用较新的
迭代器时,您似乎正在使用旧的
枚举类型。什么是
bean
getSession()
返回什么?当您应该使用较新的
迭代器时,您似乎正在使用旧的
枚举类型。是的!!这有帮助。非常感谢不用担心,只是希望能帮上忙是的,这很有帮助,非常感谢不用担心,只是希望能帮上忙