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

Passing a Vector From JSP to a servlet

 
Austin Lowry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Austin:
There is no addParameter() method in any ServletRequest-type class... right?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try to use:
session.putValue("field", myvector)
and session.getValue("field")
------------
ruijin yang
SCJP2
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that get/putValue are deprecated.
If anything, use get/setAttribute (but I'm still against it )
Dave.
 
Austin Lowry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"serialise it on the page as a hidden field or something"

How do you do that?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was afraid that's what you meant. Me personally, there's no way that I'll ever do that
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic