This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes getRequestDispatcher() clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getRequestDispatcher() clarification" Watch "getRequestDispatcher() clarification" New topic

getRequestDispatcher() clarification

Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284

Hello guys,
i have a doubt regarding (ServletRequest|ServletContext).getRequestDispatcher().

In the API is said that:

This method returns null if the servlet container cannot return a RequestDispatcher.

whereas in the Servlet v2.4 specs:

The getRequestDispatcher method takes a String argument describing a
path within the scope of the ServletContext. This path must be relative to the root
of the ServletContext and begin with a �/�. The method uses the path to look up
a servlet, using the servlet path matching rules in Chapter SRV.11, �Mapping
Requests to Servlets�, wraps it with a RequestDispatcher object, and returns the
resulting object. If no servlet can be resolved based on the given path, a
RequestDispatcher is provided that returns the content for that path

Can someone explain me why these are not contradictory?

[ September 12, 2007: Message edited by: Oricio Ocle ]

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
If no servlet can be resolved based on the given path, a
RequestDispatcher is provided that returns the content for that path.

Its not necessary that the argument to the request dispatcher is always a servlet. It may be another resource like HTML files e.g.

Hence is such a scenario, the HTML page is rendered. And of course, if getRequestDispatcher cannot return a RequestDispatcher, it will return nothing(null). Please let me know if this clarifies your doubt.

Thanks and Regards
Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284

Hello Satya, thanks for your reply.
the words that make me doubt are:
... content for that path

For example, it sounds to me like

will return the content of this folder to the user agent...
Dont you think it cant be misunderstanding?
subodh gupta
Ranch Hand

Joined: Jul 23, 2007
Posts: 203

1)In first case the request processor will return null bcoz it found no file that RequestProccessor can return to be operated upon e.g. if you pass directory in the path.

2)but if their is path that can be resovled as a valid html content it will return the content (it can be servlet/html etc).

I think this will make you clear.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60077

Originally posted by subodh gupta:
the request processor will return null bcoz ...

subodh gupta, please use real words like "because" when posting to the forums.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Yeah i also guess the same as what subodh gupta told.

If the particular path is leading to the actual file but NOT the directory, a non-null request dispatcher would be served with its contents.

Otherwise, a null request dispatcher (nothing) would be returned.

[ July 23, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
I agree. Here's the link:
subject: getRequestDispatcher() clarification
Similar Threads
Correct answer
getRequestDispatcher doubt
Distributed applications
problem with RequestDispatcher