Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference b/w RequestDispatcher in Context and request

 
Srinivas Genji
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Everyone,
THere are two request dispatcher, one is in Servlet Context and other in ServletRequest. Can anyone explain when do we use either one with an example
THanks
Cheers
 
chowdary Thammineedi
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's very clear if you glance through the Servlet API.

If you use a ServletContext to get a RequestDispatcher you MUST use a STRING starting with a "/" which is interpreted as relative to the Web-Application root.

But if you use a Request to get a RequestDispatcher then the STRING you specify in the request.getRequestDispatcher("a STRING") CAN start with either a "/" or choose not do so.

Please note the difference in the above 2 paragraphs. MUST and CAN.

In case you are using a request to get a RequestDispatcher and you start off with a "/" then it is interpreted as relative to the Web-Apllication context just as it is with a ServletContext. But if you do not start with a leading "/" then the path is interpreted as relative to the request.(i.e /web-application/reuestURI/-----)
 
Roshini Sridharan
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

u can refer the following link

http://java.boot.by/wcd-guide/ch03s05.html

Regards
Roshini
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic