[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

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?
0
afsac
Asked:
afsac
  • 4
  • 3
  • 2
  • +1
1 Solution
 
heyhey_Commented:
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)
0
 
afsacAuthor Commented:
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.......
0
 
slokCommented:
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
afsacAuthor Commented:
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
0
 
slokCommented:
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.

0
 
mbormannCommented:
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.
0
 
afsacAuthor Commented:
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.
0
 
mbormannCommented:
Well I think that you can use the 2nd approach as outlined in the book,or the last.
:(
0
 
afsacAuthor Commented:
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
0
 
mbormannCommented:
no man ,
It's 5:30 pm in afternoon here in India.
nice meeting u 2!
:(
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now