Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpServletRequest getRequestDispatcher behaving strangely

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
kranthi adari
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now. Thank you Steve.
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic