Home Servlets Servlet Context
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Servlet Context

Advertisement
ServletContext is a interface which helps us to communicate with the servlet container.

Servlet Context

     

ServletContext is a interface which helps us to communicate with the servlet container. There is only one ServletContext for the entire web application and the components of the web application can share it. The information in the ServletContext will be common to all the components. Remember that each servlet will have its own ServletConfig. The ServetContext is created by the container when the web application is deployed and after that only the context is available to each servlet in the web application.

Web application initialization:

  1. First of all the web container reads the deployment descriptor file and then creates a name/value pair for each <context-param> tag.
  2. After creating the name/value pair it creates a new instance of ServletContext.
  3. Its the responsibility of the Container to give the reference of the ServletContext to the context init parameters.
  4. The servlet and jsp which are part of the same web application can have the access of the ServletContext.

The Context init parameters are available to the entire web application not just to the single servlet like servlet init parameters.

How can we do the mapping of the Context init parameters in web.xml

<servlet>
  <servlet-name>Mapping</servlet-name>
  <servlet-class>ContextMapping</servlet-class>
</servlet>

<context-param>
  <param-name>Email</param-name>
  <param-value>admin@roseindia.net</param-value>
</context-param>


In the servlet code we will write this as

ServletContext context = getServletContext();
pw.println(context.getInitParameter("Email")
;

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 19, 2007

Ask Questions?    Discuss: Servlet Context   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
lakshmi
August 12, 2011
j2ee

This example is very nice.. Since i am in training field i can make others to understand easily.. Thank u so much...
sreekanth
November 30, 2011
good explanation

give the code for which you are giving description..it is very useful for persons........
DMCA.com