my dog learned polymorphism
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher clarfication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher clarfication" Watch "RequestDispatcher clarfication" New topic

RequestDispatcher clarfication

Garlapati Ravi
Ranch Hand

Joined: Mar 05, 2008
Posts: 171
Hi All,

I need clarification:

All the above 3 giving me the same result. i am unable to understand exactly how they(getServletContext() and request) differ.

[ May 03, 2008: Message edited by: Garla Ravi ]

Ravi Kumar
SCWCD 5 - 89%, SCJP 1.4 - 90%
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231

You can create a RequestDispatcher in either way. If your Servlet doesn't extend HttpServlet then the only way to get a RequestDispatcher is thought the ServletContext.

when you get a RequestDispatcher throught ServletContext then you MUST start the path with a /. the path is treated as relative to the current context root.
you cah check the API

Shiraz Khan
Ranch Hand

Joined: Mar 20, 2006
Posts: 51
When you get a request dispatcher from servlet context, you have to start the path with a "/" because here there is no concept of any relative path. The container will look for the resource with respect to the application root.

When you get a request dispatcher from request then, you can give the resource path with or without "/" because here there is concept of relative path (relative to the request coming from the client). If the resource path starts with a "/" then the container will look from the app root. If the resource path is without "/" the container will look with respect to the request path.
Garlapati Ravi
Ranch Hand

Joined: Mar 05, 2008
Posts: 171
Thanks for your replies, but i am not quite convinced with your answers, as i found similar replies when i google. Requesting for appropriate reply, which is not covered in book or in APIs.

Fist I will put on what i understood.

There is absolutely no difference between above two, apart from objects which the getRequestDispatcher() method is called.

From my example relative/logical paths are no were given importance, they are treated as same.

am i correct ? please clarify.
I agree. Here's the link:
subject: RequestDispatcher clarfication
It's not a secret anymore!