Servlet Context tutorial for Java beginners

What is a servletcontext

ServletContext is a configuration Object which is created when web application is started. It contains different initialization parameter that can be configured in web.xml.

Example of ServletContext with scenario

You are developing a web application where you want to access the email of administrator in whole application. You want to set this email id @ one location and every Servlet and Jsp can access that email. In that case you will take the help of ServletContext. You can add this email in servletcontext via init parameter in web.xml. Now this value (Email id of administrator) will be available to every Jsp and Servlet in the web application.

Life Cycle of ServletContext

Step 1: Servlet container reads the DD (Deployment Descriptor – web.xml) and creates the name/value string pair for each <context-param> when web application is getting started.

Step 2: Container creates the new Instance of ServletContext.

Note*: ServletContext is an Interface.

Step 3: Servlet container gives the ServletContext a reference to each name/value pair of the context init parameter.

Step 4: Every servlet and JSP in the same web application will now has access to this ServletContext.

Servlet context init parameter Syntax

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">



Difference from ServletConfig

  • ServletConfig is one per servlet while ServletContext is one per web application.
  • ServletContext is available to all servlet & jsp in web application while ServletConfig will be available only specific servlet.
  • Difference in the syntax for servletcontext and servletconfig.

What is servletcontextlistener

Now you know that you can set init parameter in ServletContext & this Servletcontext will be initialized when application starts. After initialization this servletcontext will be available to all servlet and jsp. But what if you want to perform some kind of action when context initialization/destruction process happens. This is the situation when you will take the help of servletcontextlistener.


Bullet Points

  • Only one ServletContext is available per web application.
  • Every servlet in the same web application will have access to servletcontext.
  • It is used to access application level parameter.
  • If application is in distributed environment then there will be one servletcontext per JVM.
  • ServletContext has init parameter that can be configured in web.xml.
By | 2017-07-21T22:00:34+00:00 December 20th, 2012|Servlet|24 Comments

About the Author:


  1. Michal October 19, 2013 at 3:45 pm - Reply

    Clear and useful, thanks!

  2. hema December 24, 2013 at 11:01 am - Reply

    Clear and crisp

  3. Navneet February 8, 2014 at 3:43 am - Reply

    Excellently explained, thanks.

  4. Daniel June 22, 2014 at 11:00 pm - Reply

    Thank you – well explained and easy to understand.

  5. Shubha July 8, 2014 at 8:52 am - Reply

    Easily explained. Thanks!!

  6. fazeela January 7, 2015 at 11:13 am - Reply

    good explaination

    • Vivekanand Gautam January 10, 2015 at 4:32 pm - Reply


    • Vivekanand Gautam January 10, 2015 at 4:33 pm - Reply

      Thanks for your nice word

  7. Mahesh Sharma February 11, 2015 at 6:42 pm - Reply

    Very good explanation

  8. Maulik Patel March 9, 2015 at 9:00 am - Reply

    excellent , short and nice explanation.
    Thnak you

  9. Sarita T Rudrawal May 20, 2015 at 7:20 am - Reply

    useful explanation..

  10. krishna June 26, 2015 at 5:58 am - Reply

    nice explanation….thank you!

  11. Sunil September 8, 2015 at 3:12 pm - Reply

    Nice & Easy concept…..Thanks.

  12. Bob January 7, 2016 at 12:44 am - Reply

    Thanks, i understood it now

  13. Dan March 25, 2016 at 3:58 pm - Reply

    This is the best explanation of ServletContexts and how they relate to ServiceConfigs that I’ve seen – very simple and straightforward. Other sites confused me but this was excellent. Thank you.

  14. manoj April 21, 2016 at 5:59 am - Reply


  15. Ali June 2, 2016 at 2:23 pm - Reply

    straightforward and nice.

    • JM June 13, 2016 at 12:57 pm - Reply

      Very Nice

  16. David August 26, 2016 at 7:46 am - Reply

    Great and very good explanation.

  17. Sandhya November 8, 2016 at 4:51 pm - Reply

    Very Nice

  18. PS Mehta April 24, 2017 at 10:44 am - Reply

    need more explanation

  19. Karamjit Singh Sehdev August 3, 2017 at 5:07 am - Reply

    very nice thankyou

  20. khalid August 26, 2017 at 9:29 am - Reply

    very nice thankyou

  21. Selvakumar October 28, 2017 at 10:01 am - Reply

    Good description

Leave A Comment