This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes difference between the getContextPath of ServletRequest and ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "difference between the getContextPath of ServletRequest and ServletContext" Watch "difference between the getContextPath of ServletRequest and ServletContext" New topic
Author

difference between the getContextPath of ServletRequest and ServletContext

deepakkudani menonkudani
Greenhorn

Joined: Jan 04, 2009
Posts: 3
What is the difference between the getContextPath of ServletRequest and ServletContext?
According to the java doc..
"It is possible that a servlet container may match a context by more than one context path. In such cases the HttpServletRequest.getContextPath() will return the actual context path used by the request and it may differ from the path returned by this method. The context path returned by this method should be considered as the prime or preferred context path of the application.".

Can somebody please explain the difference?

Thanks in advance.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
HttpServletRequest#getContextPath() returns the context path of the current request.
ServletContext#getContextPath() returns the context path of the webapp where the source is actually located.
deepakkudani menonkudani
Greenhorn

Joined: Jan 04, 2009
Posts: 3
Thank you Bauke for the prompt reply.

Can the values returned by the HttpServletRequest#getContextPath() and ServletContext#getContextPath() be different in any case?

Generally, we give the context path in the application.xml(In case of JBoss and Websphere).Lets say for a Test.war, we give the context path as /Test in the application.xml.
Now a call to ServletContext#getContextPath() made by a Servlet in Test.war, would get the "/Test".
Also, when a resource under the Test.war needs to be accessed,the Servlet path should be prefixed with "/Test".If the context path in the URL is differnt, then the request would not be routed to the TestWar.
So, since both the method(HttpServletRequest#getContextPath() and ServletContext#getContextPath()) return the same result, why do we have them both in the api?

Or is there any scenario for which the results of the above mentioned methods could vary?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

If my understanding is right, you can map a resource with any different URLs by specifying the URL mapping in the web.xml. Likewise, you can map the application with many different contexts. In such case, the context path would be given to you as it was in the URL address bar during the time of requesting (HttpServletRequest).

Where as the one which is returned for the invocation of ServletContext would be the ACTUAL contextpath [which is nothing but the expanded folder/directory name of your archive file (.war)] of the resource with respect to the Application deployed.

Does that help?


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]
deepakkudani menonkudani
Greenhorn

Joined: Jan 04, 2009
Posts: 3
HI Raghavan,
Thanks for the explanation. I think your explanation is right. But when i was trying in JBoss server, i found out that i could not register a war with multiple names. for eg my application.xml looks like this.

<?xml version="1.0" encoding="ISO-8859-1"?>
<application xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd" xmlns:javaee="http://java.sun.com/xml/ns/javaee">
<javaee:display-name>TestEar</javaee:display-name>
<module>
<web>
<web-uri>TestWar2.war</web-uri>
<context-root>/testwar2</context-root>
</web>
</module>
<module>
<web>
<web-uri>TestWar.war</web-uri>
<context-root>/auni</context-root>
</web>
</module>
<module>
<web>
<web-uri>TestWar.war</web-uri>
<context-root>/testwar</context-root>
</web>
</module>
</application>

In the above application.xml, I had tried registering the TestWar twice, with /auni as well as /testwar. But the server is over writing the first context(/auni) with the later.And when i access the "/auni", I get an http 404.
By doing this i feel, its not possible to register one war twice, with 2 different context names.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

deepakkudani menonkudani wrote:
In the above application.xml, I had tried registering the TestWar twice, with /auni as well as /testwar. But the server is over writing the first context(/auni) with the later.And when i access the "/auni", I get an http 404.
By doing this i feel, its not possible to register one war twice, with 2 different context names.


Seems to be a good catch, but I am not very sure. Just read the documentation for the same, you might get some insights. Let me check and get back.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between the getContextPath of ServletRequest and ServletContext
 
Similar Threads
getContextPath() in HttpServletRequest
getRequestDispatcher error in servlets
RequestDispatcher
Regarding Request Dispatcher
comparing Request Dispatchers