• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass objects through form submit

 
michael yue
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the best way to submit objects in form through POST? I want to submit an object from a jsp page to another page. There is a submit button on the page. I found out one way is to convert the object to a string to submit. If this is possible how is it done? Example codes are most appreciated.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think we can only get strings and files via the form.

wht you can do is place that particular object in the session in your first page and get the same object from the session in your next page. but it is not really appreciated.

wht is the scenerio you are having? wht kind of object you want to pass?
[ September 10, 2004: Message edited by: adeel ansari ]
 
Mark Wilcox
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the earlier post suggests - you cannot submit Java objects via HTTP form posts. Once you've displayed the results in a browser, it does not know anything about Java objects. All HTTP can do is send text - this is why Web Services (in their most common form) use XML. If it was easy to send binary encodings over standard HTTP - Web Services would be different (you'd end up with something like RMI) because binary is much more efficient than plain text.

Basically you must either uses the Session object (essentially a HashMap that maintains its values for a given user's session - where the J2EE container manages a session, usually via cookie for you automatically) OR you must persist the state yourself - for example as hidden form fields or cookies or storing in a database (where you store the record key in a cookie).

Usually people go with Session objects unless you know you're dealing with very large amounts of data in an object.

Mark
 
michael yue
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

If I use session, I cannot determine where to remove the session object after its not used anymore. The user can click on any other links in the web application. So dont know whether its advisable to leave the session there.
 
arnel nicolas
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just want your object to persist only in another page then limit your scope to "request". In that way your object will only persist per request, so you don't worry about invalidating your object.
[ September 10, 2004: Message edited by: arnel nicolas ]
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
maybe this will be usefull for you.
Look into the documentation for details
It's from interface ServletRequest
 
michael yue
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a static jsp page when i put

request.setAttribute("rptform",rptform);

it would not persist to another page cause after the page loads no object will exist. There are a 'next' link that point to another page which i require the object.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right lin.
it is meaningless when you set request attribute in your jsp page. cause on submit, that request is disposed off and a new request is instantiated.

you can use cookie or session to store your data, which you want to show on your next jsp page.

you can remove your session attribute by removeAttribute(String attr) method in HttpSession.
 
arnel nicolas
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or simply call the invalidate method of your session object.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arnel wht are you suggesting buddy. it will not just unbind the objects but invalidate the session as well.
[ September 13, 2004: Message edited by: adeel ansari ]
 
Sandro Duarte
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream s = new ObjectOutputStream(out);

BASE64Encoder e = new BASE64Encoder();

s.writeObject(yourObjectHere);
s.flush();

String youObjectAsString = e.encode(out.toByteArray());
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandro, what on earth does the code you posted have to do with the topic? Are you sure you replied to the intended topic?
 
Julian Kennedy
Ranch Hand
Posts: 823
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it (not very well ) the Base64 malarky is a way of making sure that your String (serialized Object or whatever) doesn't get whacked in transit by character encoding. Not too useful without an accompanying explanation, however.

This thread that ended up in the Java in General (Intermediate) forum may shed some light on it, particularly the posts by Mattias Arthursson and Bill Brogden.

Jules
 
Sandro Duarte
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not posting a proper explanation with the code. I didn't realize it was so obscure.

Anyway, since the original intention of lin park was to pass objects through form submit, I just posted a code we use here for doing just that



Now you can use youObjectAsString and put it in a hidden or textArea to submit it through a form.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic