This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "RequestDispatcher " Watch "RequestDispatcher " New topic
Author

RequestDispatcher

MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
There are three ways to obtain RequestDispatcher object,

1.RequestDispatcher dispatch = request.getRequestDispatcher("string")
2.RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/string")
3.RequestDispatcher dispatch = getServletContext().getNamedDispatcher("string")


can anyone explain the thrird one?how it is differ from the other two?
Should we study it for the exam?

Thanks


God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,


3.RequestDispatcher dispatch = getServletContext().getNamedDispatcher("string")


This method take the servlet name as parametere. The servlet name is definded in web.xml as <servlet-name>. The request is dispatched to that corresponding servlet.

The getRequestDispatcher methods sets the folloing request attributes while transfering the request in case of include.

javax.servlet.include.request_uri :
javax.servlet.include.context_path :
javax.servlet.include.servlet_path :
javax.servlet.include.path_info :

For the forwrod method it set the same arrtibutes , but the names have forword instead of include.

The getNamedDispatcher not set these attributes.

Hope this help

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Hi Vinod,

In addition:

1.RequestDispatcher dispatch = request.getRequestDispatcher("string")
- "string" can be a relative path or not to a particular resource. (i.e. jsp, servlet, etc.)
2.RequestDispatcher dispatch = getServletContext().getRequestDispatcher("/string")
- "/string" must NOT be a relative path. Which means you must start with a forward slash always...
3.RequestDispatcher dispatch = getServletContext().getNamedDispatcher("string")
- "string" must be a servlet name defined in the DD.

Hope this somehow helps.


"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)
MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
Good explanation!!

Thanks Narendra,Richard.
 
Don't get me started about those stupid light bulbs.
 
subject: RequestDispatcher
 
Similar Threads
Not displaying images when using RequestDispatcher.forward() for passing control from
Java Servlets
ResultSet from Servlet to JSP
How to getParameter value
jsp-config does not appear to be "shared"