Is there a way to repeat through HttpServletRequest.getAttributeNames () several times?

I am trying to register the contents of an HttpServletRequest attribute collection. I need to do this when the servlet first starts, and again right before the servlet finishes. I do this while trying to understand a cruel and poorly maintained servlet. Since I need to influence as little as possible, servlet filters are not an option.

So here is the problem. When the servlet starts, I will iterate over the numbering returned by HttpServletRequest.getAttributeNames (). However, when I want to repeat it again, getAttributeNames (). HasMoreElements () returns false! I can not find a way to list "reset". What's worse, even if I add attributes to the collection using HttpServletRequest.setAttribute (), I still get the result "false" when I call getAttributeNames (). HasMoreElements ().

Is it really possible? Is there no way to iterate over attribute names more than once?

Upon request, here is my code. It's pretty simple - don’t think that I am doing any funny things.

/** * * 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 + "}"; } } 
+4
source share
1 answer

Perhaps you should send the code to which you call HttpServletRequest.setAttribute() .

At the moment, it seems that your cruel and poorly serviced servlet removes the attributes between your two getAttributeNames() calls, but without any code examples that are hard to say.

UPDATE

Nothing in the code jumps at me as a fault ... so I created a very simple test script inside handleRequest() and gave it a twist (using jboss-eap-4.3 as my container). At first I had to manually set the attribute, since I understand the request attributes, they are always set on the server side (i.e. if I did not set it, then I did not get any output, since 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()); } 

Exit

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

So, maybe your container does not implement getAttributeNames() correctly? Perhaps try a very simple test case, for example directly in handleRequest() or doGet()/doPost() .

+16
source

All Articles