File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Calling getRequestDispatcher method ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling getRequestDispatcher method ??" Watch "Calling getRequestDispatcher method ??" New topic

Calling getRequestDispatcher method ??

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342

I am confused about calling getRequestDispatcher method we can call this method in two below ways :

RequestDispatcher dispatcher = this.getServletConfig().getServletContext().getRequestDispatcher("/servlet/Test");
if (dispatcher != null)
dispatcher.forward(req, res);

and another way is :

RequestDispatcher dispatcher = req.getRequestDispatcher("/servlet/Test");
if (dispatcher != null)
dispatcher.forward(req, res);

where req is the object of HttpServletRequest.

Can any one please explain me what is the difference between this two ways.

Thanks & Regards
Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
the HttpServletRequest.getRequestDispatcher()could take a relative path.
quote from the api: "The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher."

the ServletContext.getRequestDispatcher() could only take an absolute path.
quote from the api: "The pathname must begin with a "/" and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns null if the ServletContext cannot return a RequestDispatcher."

Win the opportunity to make money on the Internet<br /><a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: Calling getRequestDispatcher method ??
It's not a secret anymore!