wood burning stoves 2.0*
The moose likes Servlets and the fly likes HttpServletRequest getRequestDispatcher  behaving strangely Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpServletRequest getRequestDispatcher  behaving strangely" Watch "HttpServletRequest getRequestDispatcher  behaving strangely" New topic
Author

HttpServletRequest getRequestDispatcher behaving strangely

kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
Please find the code snipped below which is working perfectly eventhough I donot have any directory called Receiver under my context root(webapps/myApplication). Can you please help me how it is working.

// Obtain a RequestDispatcher (for the Receiver servlet)
RequestDispatcher rd = request.getRequestDispatcher("/Receiver/pathInfo?fruit=orange");

Also I dont have any Servlet called pathInfo under any directory of my context-root.

As far as I know getRequestDispatcher() argument starting with "/" will try to look for a file from context-root. Please do help me in this.


krad
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

It doesn't have to be a specific file in a real location, it is a URL. You should look into the web.xml file to see what servlet is mapped to that URL.

Other things to check would be a control servlet mapped to /* or /Receiver/* that could then look at the URL and translate the rest of the input as parameters to determine which page to display.


Steve
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
Thank you very much. But I read that getNamedDispatcher is the one which will look into web.xml for a given servlet name. Do you mean to say that getRequestDispatcher() method looks for a matching url-pattern where as getNamedDispatcher() looks for a servlet-name in web.xml.

So, Can we conclude that both the methods looks into web.xml for finding a sevlet.

getRequestDispatcher() for matching url-pattern
getNamedDispatcher() for matching servlet-name.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Yes. That is right. getRequestDispatcher() will lookup a URL and doesn't need to know where the URL came from - either the address of a physical file under the context root or a URL pattern that is matched in web.xml. The getNamedDispatcher() will look for Servlet names which usually come from the web.xml.
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
Thank you. What if the getRequestDispatcher() method finds requested resource both under context-root and in web.xml but each having different content. Which one will take precedence.

Say for example a file with name test.jsp under context-root as well as under context-root/WEB-INF (with an entry with url-pattern /test* in web.xml) but each having different content.

Which one will take precedence.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

So these kinds of questions can best be answered by reading the Servlet specifications, which you can find here.

The appropriate chapter is SRV 11: Mapping Servlets to Requests. If you read section SRV.11.2 you will see:


So when you have the URL mapped to:
/test*
That would be an unmatched pattern since the * would not be a wildcard, but must be an exact match. So let's change your question to ask:
"When I make a request to /test/my.jsp, which will be chosen between a JSP with the path under the context root of /test/my.jsp versus a servlet mapped to /test/*?"

To answer this we look at section SRV.11.1 which says:


So in the case of a JSP at:
/ContextRoot/test/my.jsp
versus a Servlet mapped to
/test/*

First try Rule 1, and if there isn't a specific match to /test/my.jsp then that fails.

Then try Rule 2... by looking through the mapped URLs, /test/my.jsp matches the /test/* URL so that servlet gets called.

Only if the requested URL didn't match a specific mapping in the web.xml would Rule 3 apply and let the JSP servlet get executed. But because Rule 2 applies before Rule 3, those URLs you map in the web.xml will take precedence over extension-mapped URLs, such as JSPs.
[ December 31, 2008: Message edited by: Steve Luke ]
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
I got it now. Thank you Steve.
 
jQuery in Action, 2nd edition
 
subject: HttpServletRequest getRequestDispatcher behaving strangely