• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Vector object in HttpServletRequest

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 42162
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic