File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Pass objects through form submit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Pass objects through form submit" Watch "Pass objects through form submit" New topic
Author

Pass objects through form submit

michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Sep 01, 2004
Posts: 6
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

Joined: Nov 20, 2003
Posts: 204
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

Joined: Dec 16, 2003
Posts: 149
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

Joined: Aug 04, 2004
Posts: 724
Hi
maybe this will be usefull for you.
Look into the documentation for details
It's from interface ServletRequest


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Dec 16, 2003
Posts: 149
or simply call the invalidate method of your session object.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Sep 13, 2004
Posts: 2
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

Joined: Jan 10, 2002
Posts: 61084
    
  66

Sandro, what on earth does the code you posted have to do with the topic? Are you sure you replied to the intended topic?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
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

Joined: Sep 13, 2004
Posts: 2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass objects through form submit