participate


Java Servlet - What is the difference between ServleRequest.getRequestDispatcher() and Ser
<<   Back to Forum  |   Give us Feedback
This topic has 3 replies on 1 page.
venkadeshkumaresan
Posts:13
Registered: 2/1/05
What is the difference between ServleRequest.getRequestDispatcher() and Ser   
Aug 3, 2005 10:01 PM

 
Hi To All

What is the difference between ServleRequest.getRequestDispatcher() and ServletContext.getRequestDispatcher()

regards
venkadesh
 
Madathil_Prasad
Posts:2,309
Registered: 12/16/04
Re: What is the difference between ServleRequest.getRequestDispatcher() and Ser   
Aug 3, 2005 11:05 PM (reply 1 of 3)  (In reply to original post )

 
from javadoc

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(java.lang.String path)Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
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 difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.

What this means is that with the ServletContext's RequestDispatcher, the path is always set to the root of the context , ie '/' as compared to the Request's RequestDispatcher where you could specify a path relative to the resource servicing that request.

A simple example

Assume the resource that's servicing the current request is servlet/FirstServlet

Assume, i need to get to a resource /servlet/someOtherServlet using a request disptacher.

   RequestDispatcher rd = getServletContext().getRequestDispatcher("/servlet/someOtherServlet");
   rd.include(request, response);
 
Alternatively
 
    RequestDispatcher rd = request.getRequestDispatcher("someOtherServlet"); //path relative to firstServlet
   rd.include(request, response);


cheers,
ram.
 
man_bsl
Posts:2
Registered: 4/30/08
Re: What is the difference between ServleRequest.getRequestDispatcher() and   
Apr 30, 2008 3:47 AM (reply 2 of 3)  (In reply to original post )

 
servletRequest.getRequestDispatcher("\relative Path") takes relative path( means current working directory).

But servletContext.getRequestDispatcher("\absolute Path") takes absolute path( means with root path)
 
evnafets
Posts:10,798
Registered: 29/04/03
Re: What is the difference between ServleRequest.getRequestDispatcher() and   
Apr 30, 2008 7:44 PM (reply 3 of 3)  (In reply to #2 )

 
Congratulations man_bsl on providing a redundant answer to a post almost three years old.

Was there a reason to revive this thread from the depths and say almost exactly the same thing, but slightly wrong?
 
This topic has 3 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 129

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums