File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Diff  getNamedDispatcher() & getRequestDispatcher() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Diff  getNamedDispatcher() & getRequestDispatcher()" Watch "Diff  getNamedDispatcher() & getRequestDispatcher()" New topic

Diff getNamedDispatcher() & getRequestDispatcher()

Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86
Can anybody explain whats the Difference between getNamedDispatcher() &
getRequestDispatcher() methods of javax.servlet.ServletContext Interface.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 951

Hope this Help


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75

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.


It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
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

Joined: Apr 10, 2006
Posts: 75

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


Steven Colley
Ranch Hand

Joined: Feb 18, 2005
Posts: 290
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.




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

Hope it help you!!


SCJP | SCWCD | SCBCD | SCWSD 5 | SCEA (I) 1.4 | SCEA 5 | IBM SOA 669
I agree. Here's the link:
subject: Diff getNamedDispatcher() & getRequestDispatcher()
jQuery in Action, 3rd edition