This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Difference between getServlet() & getRequestDispatcher() 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 "Difference between getServlet() & getRequestDispatcher()" Watch "Difference between getServlet() & getRequestDispatcher()" New topic
Author

Difference between getServlet() & getRequestDispatcher()

Raghavendra Holla
Ranch Hand

Joined: Jun 02, 2000
Posts: 58
Hello,
I am confused with above two usage and their advantage over others. By doing

1) RequestDispatcher rd = getServletContext().getRequestDispatcher("/servlet/emSessionInit");
rd.forward(request,response);
and

2) emSessionInit ems = (BookDBServlet) getServletConfig().getServletContext().getServlet("emSessionInit");
ems.doPost(request,response);
My doubts are
a) What are limitations of 1 & 2, which context they can be used.
b) Can I do (2), i.e. By getting handle to servlet can i call doPost method of that servlet?
c) Is it possible to call any other method of servlet by using request dispacther object?
d) Clearly it is not possible to call servlet residing in diffrent server than the current server by 2, I am correct ?
e) When I use forward or include of RequestDispatch object what methods of calling servlet called, is it determined by what method in request object?

Sorry ... the list is big.. But if anybody can help me it would be great help for me. I think these quesion has to be answered by experts, Frank & Bill any help from you end?
Thanks once again,
reagrds,
Holla.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
There is one important distinction. Option 2, getServlet() simply won't work in servlet containers conforming to version 2.2 or later of the Servlet API. getServlet has been deprecated and defined to always return null, so you have to use the RequestDispatcher interface.
This only leaves your question e), and I'm not sure about that one. But check the Servlet API documentation at Sun's web site.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between getServlet() & getRequestDispatcher()
 
Similar Threads
Calling getRequestDispatcher method ??
RequestDispatcher doesn't work
Please help me in JRUN
requestdispatch - servlet to servlet in iplanet( anyone plzzzzzzz!!!!)
request dispatcher include call failed