• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing parameters between 2 servlets

 
Zein Nunna
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set parameters and do a RequestDispatch to the target servlet

Regards
Balaji
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zein Nunna
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben,

Works like a charm.



Zein
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic