Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher's getRequestDispatcher()

 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, If we get RequestDispatcher object through HttpServletRequest object like, we are giving relative path. If we get RequestDispacher object through ServletContext object like, we must specify with '/'.

Can you please explain why we are using '/' incase of ServletContext's method getRequestDispatcher()? Thankyou.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What does javadoc say
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. The document says
If the path begins with a "/" it is interpreted as relative to the current context root.
. Eventhough if we are using "/" it is interpreting as relative path, as incase of, RequestDispatcher view=request.getRequestDispatcher("result.jsp") does. Then why we have to use "/"(anyway it is not going outside the current application)? Can you please explain? Thankyou.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


ServletRequest can have relative paths , where as ServletContext can't. It has to start with "/" which means web app context root.

say your servlet url pattern is as below

<url-pattern>/login/ProcessLogin</url-pattern>

and say forward to a jsp , if you dont use "/" , it assumes to refer inside /login and search for jsp inside login directory. Use of "/" make sure that you are referencing from Context path.
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou, Balu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic