File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Questions about RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Questions about RequestDispatcher" Watch "Questions about RequestDispatcher" New topic

Questions about RequestDispatcher

Timotius Pamungkas

Joined: Feb 26, 2005
Posts: 28

I've just read a book that describes a little about RequestDispatcher. It is said that both ServletContext and ServletRequest has method getRequestDispatcher() that returns RequestDispatcher, but it doesn't explains any specific behaviour about this (Request Dispatcher scope or life cycle or something like that). Actually, what is the difference between them?

Another question. It is said that RequestDispatcher has method forward() and include(). What is the difference between them? Both has same functionality, isn't it? After all, one of them can be used to forwarding request to another servlet that designed for specific task after partially processed in current servlet (who calls forward or include). After all, why should we need forward() or include()? Why dont just servlet X calls the doXXX() method of servlet Y instead of call forward() or include()? After all, the doXXX() is always public...

Thanks in advance...
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

About your first question, you have to use an absolute path with ServletContext to get a RequestDispatcher. Whereas ServletRequest allows you to use relative paths too (relative to the current servlet).

[My Blog]
All roads lead to JavaRanch
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
What is the difference between them? Both has same functionality, isn't it?

forward() tells RequestDispatcher to clear any response already written and have another servlet process the entire response for the current request. Once the destination servlet is done the response is complete. Control will not return to the current servlet.

include() tells the RequestDispatcher to run another servlet, write any output to the response and return to the current servlet.

forward() is a one-way trip to another servlet. include() will return control to the current servlet.

Why dont just servlet X calls the doXXX() method of servlet Y instead of call forward() or include()?

There are many reasons you don't want to use the doXXX methods directly. Here are a few:

By calling servlet methods directly you are making assumptions about which servlet should process the request. By using include() or forward() you let the servlet container map the target url to a servlet.

The servlet container manages the lifecycle of servlets. To call the doXXX methods directly you would have to instantiate your own instance of the servlet. That's not a best practice.

If you call doGet() and doPost() directly, you avoid the normal processing that precedes running a servlet such as filters, security, etc.

Finally, calling doGet() and doPost() directly makes an assumption that the servlet is an HTTP servlet.
[ October 31, 2006: Message edited by: Scott Johnson ]
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
One more reason why foward() should be routed through servlet container is let the configured listener's/filters be invoked as the request moves to target servlet.
I agree. Here's the link:
subject: Questions about RequestDispatcher
It's not a secret anymore!