Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing variables between servlets

 
Chak Terlapu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic