File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes diffrence b/w calling of getRequestDispatcher form request and getServletCotext. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "diffrence b/w calling of getRequestDispatcher form request and getServletCotext." Watch "diffrence b/w calling of getRequestDispatcher form request and getServletCotext." New topic
Author

diffrence b/w calling of getRequestDispatcher form request and getServletCotext.

Chidanand Chauhan
Ranch Hand

Joined: Nov 10, 2005
Posts: 77
Can anybody tell me the difference by calling the getRequestDispatcher() by request object and ServletContext object.

Please tell me the difference reqarding the scope of the two object

I suppose there is only the differnce of the scope of the request and response.
One diff is using request object request.getRequestDispatcher(path). we pass argument as relative path as well as the root of the application through /(forward slash)
But in ServletContext we only call forward through /(forward slash) for the path.
Please give the explaination regarding the scope of the two object.

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Chidanand Chauhan:

One diff is using request object request.getRequestDispatcher(path). we pass argument as relative path as well as the root of the application through /(forward slash)
But in ServletContext we only call forward through /(forward slash) for the path.
Please give the explaination regarding the scope of the two object.



You just did.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Chidanand Chauhan
Ranch Hand

Joined: Nov 10, 2005
Posts: 77
Then is there any concept of request object and ServletContext object.
like request is a thread safe and context not related to this i am talking about this
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Take a look at the methods of RequestDispatcher:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/RequestDispatcher.html

Hint:
Notice that they both require a servletRequest and servletResponse object as arguments.
What does this say about thread safety?
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

What does this say about thread safety?


I couldn't find a mention about thread safety in the docs.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

My point was that, no matter how you obtain the requestDispatcher, you have to pass it a reference to the current request whenever you want to use it.
Since doing so limits it's use to a particular request, it doesn't really matter if you get the dispatcher from the request itself or from the servlet
context.

The real difference is the one that you pointed out.
suppose there is only the differnce of the scope of the request and response.
One diff is using request object request.getRequestDispatcher(path). we pass argument as relative path as well as the root of the application through /(forward slash)
But in ServletContext we only call forward through /(forward slash) for the path.

[ December 24, 2005: Message edited by: Ben Souther ]
Chidanand Chauhan
Ranch Hand

Joined: Nov 10, 2005
Posts: 77
Originally posted by Ben Souther:
My point was that, no matter how you obtain the requestDispatcher, you have to pass it a reference to the current request whenever you want to use it.
Since doing so limits it's use to a particular request, it doesn't really matter if you get the dispatcher from the request itself or from the servlet
context.




[ December 24, 2005: Message edited by: Ben Souther ]


Hi Ben

Thanks But my question is why did they provide this method through context or request object.

Suppose if i call this method through ServletRequest then what is the diff between ServletContext object. Is it related to the Thread safety or not. Please guide for this.

Thanks
Chidanand Chauhan
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Chidanand Chauhan:
Suppose if i call this method through ServletRequest then what is the diff between ServletContext object. Is it related to the Thread safety or not.


No, it is not related to thread-safty here. And for why?? you can refer to Ben's last reply, prior my post, in this thread.
Chidanand Chauhan
Ranch Hand

Joined: Nov 10, 2005
Posts: 77
Thanks, atlast i found that its not related to thread safety.
Thanks to Ben for giving the excellent explanation.

Thanks,
Chidanand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: diffrence b/w calling of getRequestDispatcher form request and getServletCotext.