GeeCON Prague 2014*
The moose likes Servlets and the fly likes Vector object in HttpServletRequest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Vector object in HttpServletRequest" Watch "Vector object in HttpServletRequest" New topic
Author

Vector object in HttpServletRequest

raj sekhar
Ranch Hand

Joined: Oct 16, 2001
Posts: 117
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

Joined: May 26, 2003
Posts: 30586
    
154

Raj,
Where is getTestVector() defined. Where does it get the Vector from? Is it a local, instance or static variable?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
raj sekhar
Ranch Hand

Joined: Oct 16, 2001
Posts: 117
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

Joined: Jan 10, 2002
Posts: 61314
    
  66

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
 
GeeCON Prague 2014
 
subject: Vector object in HttpServletRequest