wood burning stoves 2.0*
The moose likes Servlets and the fly likes Sharing variables between servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sharing variables between servlets" Watch "Sharing variables between servlets" New topic
Author

Sharing variables between servlets

Chak Terlapu
Ranch Hand

Joined: Oct 20, 2000
Posts: 32
Hi,
I have two servlets A and B. When a button in Servlet A has been clicked servlet B gets invoked. I have a string array in servlet A which I want to use in servlet B. Could anyone tell me how I can do it. Is it possible?
Thanks
Chak
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Assuming both sevlets are in the same application, passing around objects like arrays of strings is what HttpSession objects are for.
Incidently, saying something like:
"When a button in Servlet A has been clicked servlet B gets invoked."
represents sloppy thinking - servlet A may make a HTML page that has a button which can be clicked, but servlet A does not contain a button. You should always keep in mind that a servlet just gets a request and creates a response - any connection between repeated requests has to be created by the programmer.
Bill

------------------
author of:
Heath Lilley
Ranch Hand

Joined: Jan 09, 2001
Posts: 72
2 ways...
1. Before you get to servlet A is it possible to put this String in to the session...

Then get the array out of the session in servlet B.

2. You could try outputing the array into a hidden field in the page so that it gets submitted to servlet B. This is messy and a "bad practice" but it will work...usually.
Hope this helps.
[This message has been edited by Heath Lilley (edited August 21, 2001).]
Chak Terlapu
Ranch Hand

Joined: Oct 20, 2000
Posts: 32
Hi,
Thanks for the reply and I am sorry it was a mistake to mention "When button in Servlet A..." I have another question.
I have two servlets A and B. When a button in HTML page created by Servlet A has been clicked the control goes to another HTML page which contains a button, when clicked invokes servlet B. Now how can I make the control go from Servlet B to servlet A after the doPOst method is executed in Servlet B. The doPOST method in servlet B does a database operation. Can I add a statement at the end of doPOST method such that after executing the database operation the page created by servlet A gets displayed.
Thanks
Chak
Heath Lilley
Ranch Hand

Joined: Jan 09, 2001
Posts: 72
I think i understand your question ...
You want to direct the user to servlet A from servlet B.
If this is what you want to do, you need to look at the following object.method.

You can send the request and response to any servlet/jsp you want by using this method...as long as the servlet/jsp is in the same web application.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sharing variables between servlets