Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cmmunication between two servlets?

 
Tom Barns
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I'm trying to implement communication between two servlets,but
the problem that I'm facing is depracated methods as getServlet("name")
and others ,so please if you have two very simple servlets to show how they communicate, that will be great.
thanks
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Barns:
I'm trying to implement communication between two servlets,but
the problem that I'm facing is depracated methods as getServlet("name") and others

I don't have sample code for you, unfortunately, but the gist is this. Sun deprecated getServlet because the direct servlet-servlet interaction it allows encourages bad practice.
If you want two servlets to communicate, one servlet can forward a request to the other (getServletConfig().getServletContext().getRequestDispatcher("/secondServlet").forward(req, res)). Any parameters that need to be communicated can be incorporated as part of the request (e.g. req.setAttribute("name", myObject)).
Sometimes the relevant state may be session-scoped (req.getSession().setAttribute("name", myObject)) or even application-scoped (getServletConfig().getServletContext().setAttribute("name", myObject)). Carefully synchronize any thread-unsafe code though.
If a forward doesn't fit the bill, you can also use include. Should neither model be what you want, then there is probably some shared functionality in your servlets that really wants to be factored out into a bean or other Java object.
- Peter

[This message has been edited by Peter den Haan (edited February 12, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic