RequestDispatcher

I have a query regardin servlets. why should 1 say request.getRequestDispatcher() ? Why is HttpServletRequest object used here? how is this same as getServletContext().getRequestDispatcher()
[197 byte] By [rip4rra] at [2007-10-3 11:22:12]
# 1

The request.GetRequestDispatcher :

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.

The servletContext.gerRequestDispatcher :

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.

tolmanka at 2007-7-15 13:47:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
The API says"For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource." what does this mean?
rip4rra at 2007-7-15 13:47:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

In the Servlet you can call the request.getRequestURL that will return the URL made to call the servlet.

In servletA you call getRequestURL and it returns http://localhost/myApp/servletA

You call getRequestDispatcher("servletB")

You do the forward

In servletB you call getRequestURL and it returns http://localhost/myApp/servletB

This is because servletA may use servletContex.getRequestDispatcher and servletB may then use request.getRequestDispatcher so the request object must accurately reflect servletB's context

tolmanka at 2007-7-15 13:47:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...