• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it true? (about getRequestDispatcher())

 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletContext.getRequestDispatcher("/MyTestServlet.do")


I want to know that the ServletContext's getRequestDispatcher() method will not take URL pattern of a servlet as a parameter?
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know that the ServletContext's getRequestDispatcher() method will not take URL pattern of a servlet as a parameter?

Your question is not quite clear...however, if you're looking for a similar method which does not take a url-pattern as an argument....it is ServletContext.getNamedDispatcher(String ServletName) where ServletName is the name you've declared in the DD for the servlet
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyServlet.TestSession

web.xml


RequestDispatcher rd = getServletContext().getRequestDispatcher("red");
rd.forward(request,response);


it will not work because we must pass the url starting with / to ServletContext's getRequestDispatcher() method.right?

But if want to invoke a servlet through url pattern then you must avoid the / before the url paramete.

RequestDispatcher rd = request.getRequestDispatcher("red");
rd.forward(request,response);


it works fine.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if want to invoke a servlet through url pattern then you must avoid the / before the url paramete.


Where do you want to avoid that? You're not supposed to avoid using "/" if you're ServletContext.getRequestDiapatcher() method.

Therefore
RequestDispatcher rd = getServletContext().getRequestDispatcher("/red");
rd.forward(request,response);
Would work fine here
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Sayak, it will not invoke the RedServlet servlet if put "/red".
RequestDispatcher rd = getServletContext().getRequestDispatcher("/red");


Its will try to locate any resource like [Application Path]/red

Please run my code, you will get error.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its will try to locate any resource like [Application Path]/red

That's what it is supposed to do.... "/" means the context root(application)...By the way I ran the code with
RequestDispatcher rd = getServletContext().getRequestDispatcher("/red");
and got no errors...it's workin' fine...check if you're goin' wrong somewhere
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Sayak its working. You make my eye open. Thank you.
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the following is working:


RequestDispatcher rd = getServletContext().getRequestDispatcher("/red");


because "/red" matches pattern "/red/*" that's declared in web.xml above, right?

Or is it working because it looks in the Context and finds "red" there with disregards with the DD above?

Thanks
[ December 23, 2006: Message edited by: Firas Zuriekat ]
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The former one
[ December 23, 2006: Message edited by: Sayak Banerjee ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic