File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Passing an object as a part of Form Post - Possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing an object as a part of Form Post - Possible? " Watch "Passing an object as a part of Form Post - Possible? " New topic
Author

Passing an object as a part of Form Post - Possible?

Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi all,
Can we pass an object as part of a form post from an html page? I would like to create an Object using my JavaBean and pass it to the JSP along with the form fields.
Is this possible?
Thanks in advance,


------------------
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
how are you going to embbed (so to speak) the object inside of a form field ??..there are only so many types of attributes that a form can have (text, radio etc)
but if u think you can do that then its no problem coz any data inside of a form can be posted to the server
you can use file upload as an example, its almost always inside of a form (just with a diff encoding type) and when its posted you can easily read the content off the input stream...
-manav
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi manav,
Am sorry for not making my query comprehensive.
You see, if we use METHOD=POST, there is no restriction on the data that could be posted to the server.I am assuming also, that any type of objects could be posted to the server.Correct me if I am wrong.
We generally post the form fields to the server.I wanted to explore, if I can also post some objects apart from the form field data.The object could be made available through the JavaBean in the JSP page.
So,my query is - "is posting form field data PLUS object(s) created by the JavaBean method to the server possible?"
Regards,
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56

  5. [This message has been edited by Desai Sandeep (edited April 26, 2001).]
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
anything you pump into the output stream can be read at the server..how the 0bject is packed and unpacked is upto you
but you see at a more basic level form can only have certain attributes i don't see how you can have a object inside a form..we use forms to pass data to the server (instead of using the session object) but those are basic datatypes like strings and numbers (stuff form fields can contain)...
i hope i communicated well?
-manav
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
You are correct when you say that you can pass any kind of data in a POST input stream. However, to put a Java object in the stream, you need a Java Virtual Machine running on the client. With an applet, you have a VM. But when a browser is displaying an HTML form, where is the VM?
------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
Website: http://www.philhanna.com


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Why do you want to pass a java object THROUGH a form from one jsp to another? Generally HTML form has some standard set of input controls like TEXTFIELD,RADIO,CHECKBOX,HIDDEN,SUBMIT,LIST etc. If you want to pass an object from one jsp to another use request,session,application scoped vars.
May be we don't really get what's your real requirement is.
regds
maha anna
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi all,
Thanks very much for the response.
We had a very simple requirement to pass the Resultset Object, which was obtained by one of the methods of the Java Bean.This had to be posted along with the Formfield data.
The Resultset Object along with the form data was required by another JSP.We had 2 options to do it.

  1. Process the Resultset in first JSP itself.
  2. Use the setAttribute() of request,session or application implicit objects, as Maha anna has suggested
  3. Pass the Resultset object to another JSP along with the form fields.

  4. Wanted to explore the third option, as our second JSP was specifically designed keeping this in mind.
    Our problem was how to append that Resultset Object to the URL, after the user enters the form field data, i.e. how do I trap the URL when the user clicks "Submit",append the Resultset object in this fashion...

    ...and then post the data to another JSP.
    Can this be done?Or have I missed something?
    Thanks for your views.
    Regards,
    Sandeep Desai
    vgdesai@bom3.vsnl.net.in

    1. Sun Certified Java Programmer Scored 93 per cent
    2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
    3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
    4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56

    5. [This message has been edited by Desai Sandeep (edited April 27, 2001).]
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
Originally posted by Desai Sandeep:
[B]
I think u can do with proper type casting.
Anil


SCJP 1.5, SCEA, ICED (287,484,486)
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
Can we trap the URL after the user clicks the Submit button and before the data is posted to the other JSP?
Thanks in advance
Regards,

------------------
Sandeep Desai
vgdesai@bom3.vsnl.net.in

  1. Sun Certified Java Programmer Scored 93 per cent
  2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
  3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
  4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56

  5. [This message has been edited by Desai Sandeep (edited April 27, 2001).]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Desai Sandeep:
We had a very simple requirement to pass the Resultset Object, which was obtained by one of the methods of the Java Bean.

It is bad practice to allow "live", connected ResultSet objects to venture anywhere outside a try... finally block. If you must, use a disconnected buffer such as a Collection or a javax.sql.CachedRowSet.
There certainly cannot be any question of passing a ResultSet between JVMs, let alone between a server and a browser and back. Even if a ResultSet were Serializable -- it is not -- all the state of the database connection would be transient, and lost upon deserializing the ResultSet.


  1. Process the Resultset in first JSP itself.
  2. Use the setAttribute() of request,session or application implicit objects, as Maha anna has suggested
  3. Pass the Resultset object to another JSP along with the form fields.


The first option is preferable by far. The second works, but violates good practice as indicated above, because (a) it is a potential source of resource leaks (b) hogs a database connection for longer than necessary and (c) couples two separate Java classes (the JSPs) much too tightly. The third is impossible.
There is a fourth option -- refactoring. I don't know the details, but it sounds as if there is something fundamentally flawed about what you want to do. If your first JSP is about setting up a data query, then it should not be a JSP but a combination of a servlet and Java classes, JavaBeans, or EJBs. For example, instead of passing an open query from one JSP to the other, you could pass a request- or session-scoped query object that contains enough information and code to run the query the moment you need the data. This addresses all concerns mentioned above in one go: (a) since you delay firing the query until you actually need the data, you can protect it with a try... finally block (b) for the same reason, the connection is used as briefly as possible and (c) the query object acts as a well-defined interface between the two jsps/servlets and moves all the database access code out of them.
This is just one possible solution. There are at least half a dozen others (such as using a session EJB to hold the query state and provide a facade for the data access).
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing an object as a part of Form Post - Possible?