• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding problem in RequestDispatcher

 
Abhilasha Sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am having some understanding problem.
The following is from SRV.8.1
To allow RequestDispatcher objects to be obtained using relative paths that
are relative to the path of the current request (not relative to the root of the
ServletContext), the getRequestDispatcher method is provided in the
ServletRequest interface.
The behavior of this method is similar to the method of the same name in the
ServletContext. The servlet container uses information in the request object to
transform the given relative path against the current servlet to a complete path. For
example, in a context rooted at ’/’ and a request to /garden/tools.html, a request
dispatcher obtained via ServletRequest.getRequestDispatcher("header.html")
will behave exactly like a call to
ServletContext.getRequestDispatcher("/garden/header.html"
).


I could not understand the (underlined)example of relative path given. The request dispatcher is called from a servlet then how can the request be /garden/tools.html. Should it not be like /garden/tools.do where tools.do is the servlet mapping to the servlet having requestDispatcher.

Also i could not understand another thing. In specification it is written:
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
.


What is meaning of underlined text.
But the API says that getRequestDispatcher() method returns null if the servlet container cannot return a RequestDispatcher.

---Thanks and Regards.
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first case you are thinking it right. Generally RequestDispatcher is obtained in servlets so that path should look like a servlet path. But remember that I can map a servlet to the path /garden/tools.html or even it it is a static resource, I can put a filter on that path and that filter might obtain the RequestDispather on the request object. So we can't say that the example is wrong.

In the second case, what the spec means that if suppose I call RequestDispacher on the path /pages/myPage.html, then first the container will look if any servlet is mapped on that path, if there is no servlet mapped to that path, then a request dispatcher for the actual HTML page named myPage.html in the pages directory will be returned. If there is no myPage.html in pages directory, then null will be returned...
 
Abhilasha Sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.I got it.
Thanks Ankit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic