• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SendRedirect - RequestDispatcher

 
Srinivasan Rengan
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I have got this fundamental doubt. We know the basic difference between sendRedirect and RequestDispatcher. Is there any difference in regard to what URL these entities can access. I heard that sendRedirect can access an URL outside the existing web-app. But, requestdispatcher cannot do that.

How far is this true?

Thanx in advance!
Srini
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is correct.
sendRedirect() can use absolute URLs or relative ones. If relatives are not able to converted into absolute ones by container, it throws IllegalStateExcetpion.
req dispatcher is used to forward or include requests within a given servlet context.
You can obtain request dispatcher(rd) either from servlet context or servlet request.
If you obtain rd from servlet context you have to give path starting from "\"(root context)
If you obtain rd from servlet request you can give either strating from context root path (i call it as absolute) or relative path.
 
muthu kumaran
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible for the requestDispatcher to access a resource of another
web application , provided you can get the context of that web application

Assume there are 2 web applications

In webapplication 1 you can use
ServletContext ctx = his.getServletContext().getContext(uripath);
where uripath points to a resource in web application 2

With the context reference obtained , we can wrap the resource using the getRequestDispatcher() method of the context.

Reference:
public ServletContext getContext(java.lang.String uripath)

which says

"This method allows servlets to gain access to the context for various parts of the server, and as needed obtain RequestDispatcher objects from the context. The given path must be begin with "/", is interpreted relative to the server's document root and is matched against the context roots of other web applications hosted on this container"


muthu
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi muthu,

Though you can get context of another application using getContext() method. It usually return null. It is also stated in the spec that this method can return null for security reasons !!!

Do you have any working example ? I tried it on tomcat, but not successful.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic