本文介绍了有没有一种方式,通过HttpServletRequest.getAttributeNames()不止一次地重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录的内容的属性HttpServletRequest的集合。我需要这样做当servlet第一次启动,并再次在servlet完成权利之前。我试图理解这些混沌和虐待维护的servlet这样做。因为我需要有尽可能少的影响,Servlet过滤器是不是一种选择。

所以这里的问题。当servlet开始,我会通过()由HttpServletRequest.getAttributeNames返回枚举迭代。然而,当我想再次来遍历它,getAttributeNames()。一旦hasMoreElements()返回假!我找不到任何方式重置枚举。更糟糕的是,即使我用添加HttpServletRequest.setAttribute()属性的集合,我仍然得到的假当我打电话getAttributeNames()的结果。一旦hasMoreElements()。

这是真的可能吗?难道真的没有办法通过属性名进行迭代不止一次?

根据要求,这里是我的code。这是pretty简单 - 不要认为我做任何有趣的东西

  / **
 *
 *返回属性集合的内容,格式为InterfaceTracker loglines
 *
 * /
@燮pressWarnings(未登记)
公共静态字符串的getAttributes(HttpServletRequest的请求){
    尝试{
        StringBuilder的toLog =新的StringBuilder();        枚举attributeNames = request.getAttributeNames();        而(attributeNames.hasMoreElements()){
            串电流=(字符串)attributeNames.nextElement();            toLog.append(当前+=+ request.getAttribute(电流));            如果(attributeNames.hasMoreElements()){
                toLog.append(,);
            }
        }        回归TRACKER_ATTRIBUTES = {+ toLog.toString()+};
    }
    赶上(例外前){
        回归TRACKER_ATTRIBUTES = {+ InterfaceTrackerValues​​.DATA_UNKNOWN_EXCEPTION_THROWN +};
    }
}


解决方案

也许你应该张贴code,你叫 HttpServletRequest.setAttribute()

在这一点上,似乎你的这些混沌和虐待维护的servlet是消除您的两个电话之间属性 getAttributeNames(),但没有任何code样本这很难说。

更新

在code没有在我的印象是错误的跳出...所以我制作一个极其简单的测试箱内的handleRequest(),并给它一个洄(使用JBoss-EAP-4.3作为我的容器)。我不得不手动首先设置一个属性,作为我的要求理解的属性是它们总是设置服务器端(即,如果我没有设置它,然后,我没有得到任何输出作为枚举按 getAttributeNames()是空的)。

 了request.setAttribute(muckingwattrs,聊斋志异);枚举ATTRS = request.getAttributeNames();
而(attrs.hasMoreElements()){
    的System.out.println(attrs.nextElement());
}的System.out.println(----------------------------);枚举attrs2 = request.getAttributeNames();
而(attrs2.hasMoreElements()){
    的System.out.println(attrs2.nextElement());
}

输出

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

因此​​,也许你的容器不实施 getAttributeNames()是否正确?也许尝试直接在的handleRequest一个非常简单的测试情况下,像我这样的()的doGet()/ doPost方法()

I'm trying to log the contents of the HttpServletRequest attributes collection. I need to do this when the servlet first starts, and again right before the servlet is finished. I'm doing this in an attempt to understand a crufty and ill-maintained servlet. Because I need to have as little impact as possible, servlet filters are not an option.

So here's the problem. When the servlet starts, I'll iterate through the enumeration returned by HttpServletRequest.getAttributeNames(). However, when I want to iterate through it again, getAttributeNames().hasMoreElements() returns "false"! I can't find any way to "reset" the enumeration. What's worse is that, even if I add attributes to the collection using HttpServletRequest.setAttribute(), I still get a result of "false" when I call getAttributeNames().hasMoreElements().

Is this really possible? Is there really no way to iterate through the attribute names more than once?

By request, here's my code. It's pretty straightforward -- don't think I'm doing any funny stuff.

/**
 * 
 * Returns the contents of the Attributes collection, formatted for the InterfaceTracker loglines
 * 
 */
@SuppressWarnings("unchecked")
public static String getAttributes(HttpServletRequest request) {
    try {       
        StringBuilder toLog = new StringBuilder();  

        Enumeration attributeNames = request.getAttributeNames();           

        while(attributeNames.hasMoreElements()) {
            String current = (String) attributeNames.nextElement();

            toLog.append(current + "=" + request.getAttribute(current));            

            if(attributeNames.hasMoreElements()) {
                toLog.append(", ");
            }           
        }       

        return "TRACKER_ATTRIBUTES={"+ toLog.toString() + "}";
    }
    catch (Exception ex) {
        return "TRACKER_ATTRIBUTES={" + InterfaceTrackerValues.DATA_UNKNOWN_EXCEPTION_THROWN + "}";
    }               
}
解决方案

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().

这篇关于有没有一种方式,通过HttpServletRequest.getAttributeNames()不止一次地重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 00:14