Link to home
Start Free TrialLog in
Avatar of afsac
afsac

asked on

How do you pass variable data from one Java Servlet to another?

Question title says it all.
I have a servlet that get data from an Oracle database.
The user can then pick a piece of data.  I want to pass
what the user picks to another servlet but can figure out
how to do it.  Any takers?
Avatar of heyhey_
heyhey_

you can use HTTPSession object to attach some Java objects to the client Session or you can embed some encoded information into the generated HTML page (invisible Form Text Fields)
Avatar of afsac

ASKER

Let me try giving more information:
I have a Oracle database on a Unix machine.
I want to use Java Servlets to allow a user
to Add, Modify, and Delete records from this
table.
I have this working in a Java Applet (using jdbc)
and all works fine, except that it is very slow.
I want to show the same process using Servlets.
I have a servlet that opens the database and
retreives the "items" and displays them to
the user.  I then want the user to pick an
item and pass what they picked to another
applet that will read the database again for the
item they picked and display the rest of the record
for that item. The user will then be able to modify
the contents of the record and write it back out
to the database (using jdbc).  Like I said, this
works fine as an applet, I'm tring to speed this
project up by using servlets. I may be using the
wrong tool for the right job.......
when your Servlet1 get data from the
database and return to the user a HTML
page, you can do the following :

1. have the data be 'print' as Hypertext
links to the Servlet2
eg.
<a href="/servlet/Servlet2?DataID=123">User Choose 2</a>

or maybe

2. the data become part of a selection
in a form field like select list,
checkbox etc.
From here, you can have the Servlet2 be
the servlet that the form will post to.

Hope this helps.
Avatar of afsac

ASKER

Your suggestion (2) is what I have been using
and I have got it to work.  Please do what you
do and I will award you the points.  Thank you
for your help.

Don
ASKER CERTIFIED SOLUTION
Avatar of slok
slok

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Servlet collaboration is possible through three methods as given in the book 'Java Servlet Programming' by Jason Hunter.

(1)Through the System Properties list if on the same JVM.
System.getProperties().put("key","value");
You can get / remove them ,also as the Properties is a derived class from Hashtable you can trick the JVM and put ANY class against a String Key .

(2)thru a shared Object. A shared object can hold the pool of shared information and make it available to each servlet as needed.

There's 1 thing to watch out for when collaborating through a shared object,the garbage collector. It can reclaim the shared object any time if it isn't referenced by a loaded servlet. To do so ,it's wise for every servlet using a shared object to save a reference to the object.

(3)easiest is inheritance. Each servlet interested in collaboarting can extend the same class and inherit the same shared information.

This simlifies the code for the collaborating servlets and limits access to the shared info to the proper subclasses. The common superclass holds a reference to the shared info ,or it can hold the shared info itself.

If you are interested buy the excellent book or I can post code for (2) & (3) later tommorrow.
Avatar of afsac

ASKER

I have the above mentioned book by Jason Hunter.
It is very good and has helped me a lot. Thank you
for your input mbormann, I appreciate your time.
Well I think that you can use the 2nd approach as outlined in the book,or the last.
:(
Avatar of afsac

ASKER

I see that it is 2:33 PST, you in Cal.?
If so, what are you doing up this time of day?
You must be dedicated!  Thanx for everything!
Don
no man ,
It's 5:30 pm in afternoon here in India.
nice meeting u 2!
:(