This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
[email protected]

  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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
[email protected]

  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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    [email protected]

    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).]
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Desai Sandeep:
[B]
I think u can do with proper type casting.
Anil

 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
[email protected]

  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).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic