Scroll to top

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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
</web-app>

 

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.
Author avatar
JBT
https://javabeginnerstutorial.com/

24 comments

  1. Michal

    Clear and useful, thanks!

  2. Navneet

    Excellently explained, thanks.

  3. Daniel

    Thank you – well explained and easy to understand.

  4. Shubha

    Easily explained. Thanks!!

  5. fazeela

    good explaination

  6. Mahesh Sharma

    Very good explanation

  7. Maulik Patel

    excellent , short and nice explanation.
    Thnak you

  8. nice explanation….thank you!

  9. Sunil

    Nice & Easy concept…..Thanks.

  10. Bob

    Thanks, i understood it now

  11. Dan

    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.

  12. Ali

    straightforward and nice.

  13. David

    Great and very good explanation.

  14. PS Mehta

    need more explanation

  15. Karamjit Singh Sehdev

    very nice thankyou

  16. khalid

    very nice thankyou

  17. Selvakumar

    Good description

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.