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.
Author and all-around good cowpoke
Joined: Mar 22, 2000
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
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.
Joined: Apr 29, 2001
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)
Except that get/putValue are deprecated. If anything, use get/setAttribute (but I'm still against it ) Dave.
Joined: Aug 20, 2001
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
Joined: Jan 30, 2000
"serialise it on the page as a hidden field or something"
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.
Joined: Jan 30, 2000
I was afraid that's what you meant. Me personally, there's no way that I'll ever do that Thanks!