wood burning stoves 2.0*
The moose likes Servlets and the fly likes Passing a Vector From JSP to a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing a Vector From JSP to a servlet" Watch "Passing a Vector From JSP to a servlet" New topic
Author

Passing a Vector From JSP to a servlet

Austin Lowry
Greenhorn

Joined: Aug 20, 2001
Posts: 2
I have found this question several times, but I have not found a really good answer yet. I would like to know the best way to pass a Vector (or any object, really) from my JSP page to my servlet. I have attempted to use request.addParameter("field", data), but the request.getParameter("field") method in the servlet returns null. When I attempt to add it to the session object, a single string passes okay, but a Vector returns empty. I would pass the data via the URL, but it is possible that the number of strings I am attempting to pass will exceed the character limit. Can anyone give me a best practice on sending objects between JSPs and servlets? Thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
The session is provided for that purpose. There is no limit built in to the API on the number of objects or size of objects attached to a session.
Bill
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Austin:
There is no addParameter() method in any ServletRequest-type class... right?


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by William Brogden:
The session is provided for that purpose. There is no limit built in to the API on the number of objects or size of objects attached to a session.

IMHO - Ack!
Any data written to the session is persisted until the session is destroyed. If you do something like this for every user logged in, it doesn't take long for the server to choke. If you try to manage reading/writing and erasing data on the session it is still a nightmare due to the non-linear nature of web apps (jumpimg back 10 pages, for example)
This is just my opinion, but if you need an object between different requests either retrieve it again or serialise it on the page as a hidden field or something (I don't like this option either)
As a third option you could organise your own server side object cache so that the caching strategy is separate to the session so that it can be set to have a much shorter lifetime than the session object.
2c yet?
Dave.
ruijin yang
Ranch Hand

Joined: Apr 29, 2001
Posts: 96
Hi,
Try to use:
session.putValue("field", myvector)
and session.getValue("field")
------------
ruijin yang
SCJP2


SUN Certified Business Component Developer Java EE Platform 5 (2008)<br />SUN Certified Developer for Java Web Services (2007)<br />IBM WebSphere Portal V5.1 Application Development (2006)<br />SUN Certified Web Component Developer (2001)<br />SUN Certified Java 2 Programmer (2001)
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Except that get/putValue are deprecated.
If anything, use get/setAttribute (but I'm still against it )
Dave.
Austin Lowry
Greenhorn

Joined: Aug 20, 2001
Posts: 2
Tony, you're right about the addParameter() method, I meant setAttribute() and getAttribute(). Sorry.
Dave, I totally agree with you on not putting it in the session. This is a relativly large object whose scope is only between one JSP page and one servlet. It seems to me that I should be able to tack the Vector onto the request object being sent to the servlet. I am having a hard time understanding if this is possible, but doing so would certainly accomplish my goal of a limited life/scope method of trasnfering objects. The hidden field is possible, but I would rather take advantage of the Java language available to me.
Thanks for everyones help.
~Austin
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
"serialise it on the page as a hidden field or something"

How do you do that?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Tony Alicea:
"serialise it on the page as a hidden field or something"

How do you do that?

Make the class Serializable (if it isn't already) get the byte array representation of the stream, Base64Encode it so it can be written on the page then put it in the page as a hidden field.
When the page gets re-submitted you can reverse the process.
I'll just repeat that this isn't a solution I like but it is another valid solution to the problem.
Dave.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I was afraid that's what you meant. Me personally, there's no way that I'll ever do that
Thanks!
 
GeeCON Prague 2014
 
subject: Passing a Vector From JSP to a servlet