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.
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.
Joined: Dec 29, 2008
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.
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.
Joined: Dec 29, 2008
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.
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 ]