• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between getContext(String uri) and getServletContext()

 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to know the difference between

getContext(String url) and getServletContext()

Both of them return the ServletContext object though and one takes a path to the specified resource.
Inspite of using getServletContext(), first we can take the real path of the application and pass that path
to getContext() and get the ServletContext object. So i am confused what is the difference between two of
them? Can getContext(String url) can be used alternatively instead of getServletContext().

Thanks.
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Zaidi wrote:Inspite of using getServletContext(), first we can take the real path of the application and pass that path
to getContext() and get the ServletContext object.


What?? getServletContext() gets the context object for the current web application. There is not getContext(String uri) method in the GenericServlet class. I think it is available in ApplicationContext of tomcat. The getContext(String uri) gets the context object for the uri specified. This can be used to get a context object of a different web application...
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit, it was pretty helpful. I figured it out though by looking at the API's. In any case it was a great explanation.

Thanks
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation Ankit.

Am I right in assuming that the exam does not cover any of the API that is Tomcat specific? It's only for the Servlet API that is common across implementations?

Thanks,
Joshua Smith
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I right in assuming that the exam does not cover any of the API that is Tomcat specific?

Of course. No container dependant question.

There is not getContext(String uri) method in the GenericServlet class.

Just to make sure nobody thinks that there is no such method at all, I'd like to add that there is one here : javax.servlet.ServletContext#getContext(java.lang.String)
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe. I really need to go through the API again ...
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris and Ankit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic