File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes InitialContext vs ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "InitialContext vs ServletContext" Watch "InitialContext vs ServletContext" New topic

InitialContext vs ServletContext

James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
What's the difference between InitialContext and ServletContext, given a scenario, which one shoud be used and what principle?
What the use of <resource-ref> tag in the deployment descriptor file?
Sam Dalton
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
OK, ServletContext and InitialContext are very different things.
In a web container, each web application is associated with a context, and all resources contained within a web application exist relative to its context. For example if we have a store application, it could exist under the context /store. Therefore if the application contained a file called cart.jsp if would be accessible at http://localhost:8080/store/cart.jsp.
Each context that exists in a web container, has a special object called a ServletContext associated with it. The ServletContext represents the web applications view on the container that it is deployed within. The ServletContext allows servlets to access resources available to them in the container. The ServletContext can be thought of as a Sandbox for a web application. This sandbox allows us to have all of the benefits of isolating web applications that we mentioned above (no name clashes, and efficient easy classloading without having to set a classpath.)
The servlet context can be used to accomplish many tasks in a web application, but perhaps its primary uses are for sharing attributes between all of the servlets in an application, and for loading resources for use within the application. We also define application initialization parameters using the ServletContext, this is discussed in the section relating to deployment descriptors.

On the other hand, the InitialContext allows you to get a reference to the JNDI (or other naming service) naming service that is running on a given server (specified when you create the initial context.) This allows you to look up EJBs and other resources (JMS queues etc) that are stored in the JNDI (or other naming service)tree.
The <resource-ref> tag is used to abstract the name used in an EJB to reference anothe EJB from the JNDI name under which the bean is deployed. Check this thread for more details.
Hope this helps

<a href="" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of []Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
I agree. Here's the link:
subject: InitialContext vs ServletContext
It's not a secret anymore!