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

Diff getNamedDispatcher() & getRequestDispatcher()

 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody explain whats the Difference between getNamedDispatcher() &
getRequestDispatcher() methods of javax.servlet.ServletContext Interface.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Hope this Help

Thanks
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The difference is the accpetable values of the String argument:

ServletContext.getRequestDispatcher(String arg1)
ServletContext.getNamedDispatcher(String arg2)

arg1 must be an absolute URL (starting with a '/') targeting a valid servlet or jsp file. This URL being associated to the servlet with a <servlet-mapping> element in the DD

arg2 must be the name of a servlet, as given by the <servlet-name> sub-element of the <servlet> element of the DD.

Note that you also have

ServletRequest.getRequestDispatcher(String arg3)

=> arg3 may be both a relative (not starting with '/') or relative (to this request) URL.


Cheers
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "gaspode..."-

Welcome to JavaRanch.

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it - please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.

Enjoy your time here.
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Ok, corrected, sorry for the inconvenience. I hope the new one is acceptable

Cheers,

Svend
 
Steven Colley
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the mainly difference between these methods is that the "getNamedDispatcher()" method from (ServletContext interface) does not add some request "attributes" to the request operation. It means that the included or forwarded page are not able to get some attributes from the request sent.

Attributes:

javax.servlet.include.request_uri
javax.servlet.include.servlet_path
javax.servlet.include.context_path

javax.servlet.forward.request_uri
javax.servlet.forward.servlet_path
javax.servlet.forward.context_path
javax.servlet.forward.query_string

and another aside here:
* it does not take a forwad slash. (runtime error).

Hope it help you!!

cya!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic