Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between the getContextPath of ServletRequest and ServletContext

 
deepakkudani menonkudani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
deepakkudani menonkudani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic