Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector object in HttpServletRequest

 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Am assuming that the HttpRequest lifetime is limitd to service method. Look at the code below :
getTestVector().add("Str1");
getTestVector().add("Str2");
getTestVector().add("Str3");
req.setAttribute("StrVector",getTestVector());
RequestDispatcher reqDisp = req.getRequestDispatcher("/jsp/testVectJSP.jsp");
reqDisp.forward(req,res);

and in the jsp am retrieving the StrVector object from request using JSTL c:forEach.

<c:forEach var="item" items="${StrVector}">
The value in vector is <c ut value = "${item}"/> <br/>
</c:forEach>


The problem is everytime I reload the page on the browser, the reuslt shows cumulative repetitions of the objects in the vector.

The result when loaded thrice is: (and it increments everytime I relaod)

The value in vector is testVect1
The value in vector is testVect2
The value in vector is testVect3
The value in vector is testVect1
The value in vector is testVect2
The value in vector is testVect3
The value in vector is testVect1
The value in vector is testVect2
The value in vector is testVect3

My expectation is to have just:

The value in vector is testVect1
The value in vector is testVect2
The value in vector is testVect3

With hashtable it just prints one set but not with vector. Any specific reason ? What am I missing here ? Doesnt the request refresh everytime its invoked ?

Note: I even tried to removeAttribute("StrVector") before setting but that has no effect too.

Environment: win2K, weblogic8.1

Thanks
RS
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj,
Where is getTestVector() defined. Where does it get the Vector from? Is it a local, instance or static variable?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj, first I'd like to say that you might want to use an ArrayList instead of a Vector. A Vector is the old way of using a collection. Vector's while not deprecated, are not used anymore, the newer Collections classes are the better way to go.

Second, why not just clear it out in the beginning, or declare it just before you use it, then it is a new object each time, which is what you are trying to do. If you are trying to reuse an instance, which is what is happening, it will keep adding it and adding it to the Collection. In a HashTable, again an older Collection, use HashMap instead, it is a key-value relation, so if you have a Key "A" with the value being some Object, then add Key "A" again with a different Object, then the newer Object overwrites the first Object put into the HashMap.

Mark
 
raj sekhar
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the reply Jeanne.getTestVector()a regular 'get' method that returns a vector object which is an instance.

Mark: Thanks to u too for the reply. I agree Vector is an old way of storing but dont u think , regardless of what object I use, the web request model shd not retain it beyond the request scope ? am lost. Am gonna try to use ArrayList but am doubtful that this problem wud go away using arrayList.

Any other reasoning for this behaviour ?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any other reasoning for this behaviour ?


Re-read the responses. It is most likely your code which is retaining a reference to the Vector and causing it to be reused.
[ October 18, 2004: Message edited by: Bear Bibeault ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I can explain it a little better.

Vector/ArrayList, when you add an object you add it to the end, and does not remove any objects.

Hashtable/HashMap, if you add a Key that already exists the object that you add with it overwrites the other object

So.

Vector has 4 objects, you add and now it has 5 objects.
Hashtable has 4 objects, keys are "1", "2", "3", "4" with the mapped objects of object A, B, C, D. Now you add object E with a key of "2", so the Hashtable still only has 4 objects.

here is the final results of the Hashtable



Does that make sense.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic