• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher Doubt?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

There are two ways we can get a RequestDispatcher, the second way is through the use of ServletContext and why is that we need a forward slash when we get the request dispatcher using ServletContext??

RequestDispatcher view = getServletContext().getRequestDispatcher("\sample.jsp");
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think because the behaviour of getRequestDispatcher method of ServletCOntext is defined that way, i.e. it takes path relative to the context root.

ServletRequest defines similar method which can accept relative paths.

So, if you are using ServletContext's getRequestDispatcher, that means the method would look for the servlet wirh path relative to the context root and hence you need to put "/" in the begining of the path string.

Thanks and regards,
Saurabh
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why is this behavior?? Also what is the difference between absolute and relative path?? Anyone on this please!
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any replied guys?
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PatienceIsAVirtue
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol,

That was an excellent reply. I'll be patient till someone helps me out. Anyways, thanks!
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolute path contains the server's ip address and port for example

http://www.mysite.com:8080/MyApp/admin/pages/login.jsp

now you can use


Whereas relative path means the path relative to your web application for example in above case you web app's context is "MyApp" and the equivalent to above will be

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ali, that was crystal clear. Thanks for the explanation!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic