aspose file tools*
The moose likes Servlets and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic
Author

RequestDispatcher

sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
RequestDispatcher view
=getServletcontext().getRequestdispatcher("/result.jsp");

as you know this the request dispatcher for the context.But why it should and must start with "/".why there is no compulsion for the request.getRequestDispatcher("result.jsp");

i know that if we give "/" , it mean that search from the contextRoot of web application.

please explain ,
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

you have the Answare sandeep.

Originally posted by sandeep Talari:
i know that if we give "/" , it mean that search from the contextRoot of web application.


because here you are using servletContext to forward. so it is related to the contextroot
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
It makes sense because ServletRequest has a current request path to evaluate the relative path while ServletContext does not.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
getServletcontext().getRequestdispatcher("/result.jsp");

If you get the RequestDispatcher from the ServletContext then you cannot use a relative path (no / at the beginning of the path) because that would mean that you are saying to locate the resource relative to youself which is the ServltContext. Relative to yourself has no meaning. This is why you use the / at the beeginning of the path.

If you get a RequestDispatcher from your servlet, then the idea of relative to yourself means where on the file system the servlet is located and you can indicate a resource relative to yourself (no / at the beginning of the resource path) or relative to the web app (Context root) by starting the repurce path with a /.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61610
    
  67

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

This is, in fact, the second topic named simple "RequestDispacther" posted today.

Please go back and change your post to add a more meaningful subject by clicking the .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rohith Kumar Singh
Greenhorn

Joined: Aug 27, 2008
Posts: 6
A path with preceding '/' will be relative to the underlying context of the current web application whereas a path without preceding '/' will be relative to the current request.

Cheers,
Rohit
[ August 27, 2008: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher