What is the difference betweem getRequestDispatcher() available as part of ServletContext and ServletRequest

Questions by divya_a

Showing Answers 1 - 32 of 32 Answers

Alagu

  • Feb 23rd, 2006
 

The parameter passing to the getRequestDispatcher method of servletContext should start with "/"(relative path), where as in request it is not mantadory.

  Was this answer useful?  Yes

Aparna

  • Mar 29th, 2006
 

we use getRequestDispatcher() of ServletContext to communicate between two web applications and getRequestDispatcher() of ServletRequest is limited to one particular request.

Please any one let me know whether iam correct or wrong

Aparna

  Was this answer useful?  Yes

hsharma12

  • Apr 10th, 2006
 

Yes Aparna,

U r a bit correct. Actully getRequestDispatcher() method is used to get the reference of another servlet existing in the servletContext.

While ServletRequest interface is basically used to get infromation from the client. That client can be using any protocol like HTTP or Ftp. For these protocols we have different calsses implementing this

for example. for http--> HttpServletRequest,HttpServletResponse

thanks

Hari

  Was this answer useful?  Yes

Sen

  • May 17th, 2006
 

Definition from Sun Tutorial

You can get a RequestDispatcher object from either a request or the web context; however, the two methods have slightly different behavior. The method takes the path to the requested resource as an argument. A request can take a relative path (that is, one that does not begin with a /), but the web context requires an absolute path. If the resource is not available or if the server has not implemented a RequestDispatcher object for that type of resource, getRequestDispatcher will return null. Your servlet should be prepared to deal with this condition

  Was this answer useful?  Yes

sivamscs

  • Jul 29th, 2006
 

Hi

requestDispatcher() of a servlet context will takes absolute path.

Whereas requestDispatcher() of ServletRequest will takes relative path

  Was this answer useful?  Yes

requestDispatcher() of a servlet context will takes absolute path.

Whereas requestispatcher() of ServletRequest will takes absolute path as well as a relative path of the other resource.

  Was this answer useful?  Yes

angelina

  • Apr 11th, 2007
 

Answer from Servet API

ServletContext - getRequestDispatcher()


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 (applicaions running on two different servers). This method returns null if the ServletContext cannot return a RequestDispatcher.



ServletRequest -  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.

  Was this answer useful?  Yes

angelina

  • Apr 12th, 2007
 

I am sorry but in my above mentioned aswer I am not sure the exact meaning of foreign contexts. It might be either two applications running on two the different servers or two applications running on the same server.

statastics could be like this respectively,

1. app1 -> server1

    app2 -> server2

2. app1 + app2 -> server1

either of these two or both are right.


Anyone knows this please correct me.

Many thanks.

  Was this answer useful?  Yes

akantilal

  • Jun 12th, 2007
 

Angelina,

The 2nd one is correct. Foreign context means another application running in the same server and not in another server. Currently there is no way that you can retrieve the servlet context object for an application on another server. But also note that within the same server also, if there are security restrictions, you may not be able to get the context object for another application. In this case, when you try to get the foreign context object using the getServletContext method of ServletContext, it would return null.

  Was this answer useful?  Yes

sampra

  • Mar 4th, 2008
 


The parameter passing to the getRequestDispatcher method of servletContext should start with "/"(relative path), where as in request it is not mantadory.

  Was this answer useful?  Yes

rome2all

  • Jan 9th, 2011
 

On ServletContext there are 2 type of Request Dispatcher are there

1) getRequestDispatcher(String)

2) getNamedDispatcher(String)

and one more is there for dispatching the req to web application to another web application with in the same webcontainer(server) on for that we use foreign requestDispatcher
syntax:
{
ServletContext ct=getServletContext();
ServletContext foreignctx =ct.getContext("/webapplication name");
RequestDispatcher rd=foreignctx.getRequestDispatcher("/path/url");
rd.forward(req,res);
}
by using this we can forward our req one web application to another web application in same Container(webserver)
Note:
It did not supported by apache tomcat 6; it gave nullpointerException but weblogic 10 can
support

  Was this answer useful?  Yes

Ambar Ray

  • Sep 3rd, 2011
 

Foreign context means another application running in the same server and not
in another server. Currently there is no way I am aware off that you can
retrieve the servlet context object for an application on another server. But
yes you can retrieve within the same server, if the * security restrictions are
lifted by the web server admin, you will be able to get the context object for
another application.


But if the * security restrictions are NOT lifted by the web server admin, In
this case, when you try to get the foreign context object using the
getServletContext method of ServletContext, it would return null.


* security restrictions -- what does that mean to you ? will explain in a
minute


* security restrictions --


We need to modify the context.xml file of Tomcat. By default cross context
dispatching is disabled. We have to modify the context tag of context.xml to
enable cross context dispatching.


Make the following change in the context.xml file:



This should allow you to communicate between 2 web applications.


This is what I meant about * security restrictions.


So here is the program for you when * security restrictions are lifted by the
web server admin --


and one more is there for dispatching the req to web application to another web
application with in the same webcontainer(server) on for that we use foreign
requestDispatcher


Code
  1.  

  2. public class MyServlet extends HttpServlet implements Servlet

  3.    {@Override

  4.      protected void doPost(HttpServletRequest request,

  5.      HttpServletResponse response)

  6.      throws ServletException, IOException {    

  7.      ServletContext sc=null;      

  8.      sc=getServletContext().getContext("/ServletForwardRedirectTest");

  9.      System.out.println("sc-->"+sc.toString());

  10.      RequestDispatcher rd=sc.getRequestDispatcher("/Forward2.jsp");

  11.      rd.forward(request, response);

  12. }

  13.  

If the above modifications are not done in the Tomcat's context.xml file then
for sure when a context object using the getServletContext method of
ServletContext, it would return null.


Please revert back if you think this post is useful or let me know if any
modifications required.


Thanks,


Ambar.

  Was this answer useful?  Yes

Ambar Ray

  • Sep 4th, 2011
 

Hey Folks,


In my above post, I missed adding this line which is the MOST IMP change
above all one has to make in Tomcat's context.xml file in order to make the
above code snipet work--


Code
  1. <context crossContext="true"><br /></context>

We need to modify the context.xml file of Tomcat. By default cross context
dispatching is disabled. We have to modify the context tag of context.xml to
enable cross context dispatching using context's RequestDispatcher object.


GoodLuck,

Ambar (ray0am00).


  Was this answer useful?  Yes

Delindia fathima

  • Dec 20th, 2011
 

requestDispatcher() of a servlet context will takes absolute path Whereas requestDispatcher() of ServletRequest will takes relative path

ServletRequest has current request path to evaluate the relative path whereas context doesnot have it.If the path starts with /, then it consider the path relative to root of the webapp. If not, it consider the path, relative to the current request.

We can forward or include the request to another resource in webapplication using servletContext request dispatcher
ServletContext sc = this.getServletContext().getContext(uripath)
sc.forward(req,res);

getRequestDispatcher(String path) of the ServletRequest interface cannot extend outside the current servlet context

To summarize

If you use absolute path ("/index.jsp"), there is no difference.
If you use relative path, you must use HttpServletRequest.getRequestDispatcher().
ServletContext.getRequestDispatcher() doesnt allow it.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions