有没有办法多次迭代 HttpServletRequest.getAttributeNames() ?

发布于 2024-09-15 11:34:49 字数 1442 浏览 3 评论 0原文

我正在尝试记录 HttpServletRequest 属性集合的内容。我需要在 servlet 首次启动时执行此操作,并在 servlet 完成之前再次执行此操作。我这样做是为了尝试理解一个粗糙且维护不善的 servlet。因为我需要尽可能减少影响,所以 servlet 过滤器不是一个选项。

所以问题就在这里。当 servlet 启动时,我将迭代 HttpServletRequest.getAttributeNames() 返回的枚举。但是,当我想再次迭代它时, getAttributeNames().hasMoreElements() 返回“false”!我找不到任何方法来“重置”枚举。更糟糕的是,即使我使用 HttpServletRequest.setAttribute() 将属性添加到集合中,当我调用 getAttributeNames().hasMoreElements() 时,我仍然得到“false”结果。

这真的可能吗?真的没有办法多次迭代属性名称吗?

根据要求,这是我的代码。这非常简单——别以为我在做任何有趣的事情。

/**
 * 
 * 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 + "}";
    }               
}

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 + "}";
    }               
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别忘他 2024-09-22 11:34:49

也许您应该在调用 HttpServletRequest.setAttribute() 的位置发布代码。

此时,您的粗陋且维护不善的 servlet 似乎正在删除两次调用 getAttributeNames() 之间的属性,但如果没有任何代码示例,就很难说。

更新

你的代码中没有任何内容让我觉得有问题......所以我在handleRequest()中制作了一个非常简单的测试用例并试了一下(使用jboss -eap-4.3 作为我的容器)。我必须首先手动设置一个属性,因为我对请求属性的理解是它们总是在服务器端设置(即,如果我没有设置它,那么当 Enumeration 返回时我没有得到任何输出通过 getAttributeNames() 为空)。

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());
}

输出

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

那么也许您的容器没有正确实现 getAttributeNames() ?也许可以直接在 handleRequest()doGet()/doPost() 中尝试像我这样的极其简单的测试用例。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文