Is there a way to iterate through HttpServletRequest.getAttributeNames() more than once?

爱⌒轻易说出口 提交于 2019-12-05 06:49:34

Perhaps you should post the code where you call HttpServletRequest.setAttribute().

At this point it would seem that your crufty and ill-maintained servlet is removing attributes between your two calls to getAttributeNames(), but without any code samples it's hard to say.

UPDATE

Nothing in your code is jumping out at me as being faulty... so I crafted an extremely simple test case inside handleRequest() and gave it a whirl (using jboss-eap-4.3 as my container). I had to manually set an attribute first, as my understanding of request attributes is they are always set server side (i.e. if I didn't set it then I didn't get any output as the Enumeration returned by getAttributeNames() was empty).

request.setAttribute("muckingwattrs", "Strange");

Enumeration attrs =  request.getAttributeNames();
while(attrs.hasMoreElements()) {
    System.out.println(attrs.nextElement());
}

System.out.println("----------------------------");

Enumeration attrs2 =  request.getAttributeNames();
while(attrs2.hasMoreElements()) {
    System.out.println(attrs2.nextElement());
}

output

INFO  [STDOUT] muckingwattrs
INFO  [STDOUT] ----------------------------
INFO  [STDOUT] muckingwattrs

So perhaps your container doesn't implement getAttributeNames() correctly? Maybe try an extremely simple test case like mine directly in handleRequest() or doGet()/doPost().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!