Forums Register Login

difference between the getContextPath of ServletRequest and ServletContext

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 10994 times.
Similar Threads
getRequestDispatcher error in servlets
Regarding Request Dispatcher
comparing Request Dispatchers
getContextPath() in HttpServletRequest
RequestDispatcher
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:01:29.