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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing parameters between 2 servlets" Watch "Passing parameters between 2 servlets" New topic
Author

Passing parameters between 2 servlets

Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Guys can you tell me if it possible to pass parameters between 2 servlets.

I.e. I need do some computation in one servlet, then call another servlet, and pass it some parameters, how do i do this?

Any pointers, code examples etc.. will be great.

Thanks in advance.
Zein
Bajji Pat
Ranch Hand

Joined: Apr 05, 2005
Posts: 50
You can set parameters and do a RequestDispatch to the target servlet

Regards
Balaji
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
That depends on what you mean by "call another servlet" - do you mean forward the request, redirect the request or create an HTML page with a link to another servlet.
1. forward a request - see the setAttribute method in ServletRequest
2. redirect - rather more tricky - attach data to the session
3. html page w form - hidden form variables may be enough, session also works
I assume the other servlet is not in a different "web application".
Bill
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Could anyone please tell me what the syntax of the servlet address should be.

I have the following line of code



Where I have ..., do i need to have ..servlet/Package.servlet or what?

Both the servlets are in the same package. If you have any examples please post them.

Thanks in advance
Zein
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Will,

What I want to do is from a form, go to servlet A, do some computation there, then complete my computations in servlet B, which then redirects to some html/jsp or something.



Thanks
Zein
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What you're trying to do is called "Servlet Chaining".
If you do a Google search with those terms, you'll find plenty of examples and tutorials.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
See another problem im having with this is that in my servlet I have a parameter that I need to pass to another servlet called email. This is coded as

String email = "xyz@hotmail.com";

RequestDispatcher rdispatch = getServletContext().getRequestDispatch.....

The email isnt geting passed? It doesnt come from a form, but a database.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Add it to the request as an attribute from Servlet A and then retrieve it from the request from Servlet B



Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Thanks Ben,

Works like a charm.



Zein
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Glad it's working.
Now you understand MVC too.
Instead of forwarding to another servlet, make the last resource you forward to a JSP and do all of your formatting in there.
 
Don't get me started about those stupid light bulbs.
 
subject: Passing parameters between 2 servlets
 
Similar Threads
how to declare....and pass parameters..
Servlet auto redirect to another page
Servlet Communication
Servlet-Servlet communication
jsp using applet