Difference between ServletConfig and ServletContext in Java

Servlets » on Feb 2, 2013 { 40 Comments } By Sivateja

Let us see the main differences between ServletConfig and ServletContext, which is very popular interview question as well πŸ™‚


  • ServletConfig available in javax.servlet.*; package
  • ServletConfig object is one per servlet class
  • Object of ServletConfig will be created during initialization process of the servlet
  • This Config object is public to a particular servlet only
  • Scope: As long as a servlet is executing, ServletConfig object will be available, it will be destroyed once the servlet execution is completed.
  • We should give request explicitly, in order to create ServletConfig object for the first time
  • In web.xml – <init-param> tag will be appear under <servlet-class> tag


  • ServletContext available in javax.servlet.*; package
  • ServletContext object is global to entire web application
  • Object of ServletContext will be created at the time of web application deployment
  • Scope: As long as web application is executing, ServletContext object will be available, and it will be destroyed once the application is removed from the server.
  • ServletContext object will be available even before giving the first request
  • In web.xml – <context-param> tag will be appear under <web-app> tag

So finally…….

No. of web applicationsΒ  =Β  That many number of ServletContext objects [ 1 per web application ]
No. of servlet classes = That many number of ServletConfig objects

Cheers πŸ˜‰

​ ​​

You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


40 Responses to “Difference between ServletConfig and ServletContext in Java”
  1. what is flushing mechanism?

  2. AnilKumar says:

    Good explanation…!!

  3. prasad says:

    Nice explanation….

  4. Dnyanesh says:

    I cant understand in deeply….so….

  5. sobana says:

    Very nice Explanation .Thank you

  6. Sujith babu says:

    Very precise & good explanation Sir…..Thank you:)

  7. Jay Poojara says:

    Nice one and very helpful πŸ™‚

  8. dev says:

    buddy your explanation is so nice and clear thanks a lot

  9. nagarjuna says:

    Nice explanation but it’s better to provide example programs

  10. kushal says:

    Nice & Precise.

  11. sagar says:

    neat and precise explaination..thanks a lot

  12. srinu says:

    provide more information about servlet and jsp. frame works spring core,spring mvc

  13. Koteswararao says:

    Nice explanation

  14. Lakshmi says:

    Nice, but it would be great if you explain with a small examples…

  15. manish says:

    Nice explanation

  16. reemol says:

    crisp and clear

  17. Thens says:

    Finally, what these two methods will return to the user?

  18. anu says:

    nice and thanks

  19. shekarreddy says:

    Good explanation.Thank you!!!

  20. Antonio says:

    able understand it. Thank you..

  21. manohar says:

    do you make servletConfig param values as global param values(servletContext param values)?

  22. surya says:

    Nice explanation.Thank you!!

  23. dhanesh says:

    its nice and simple to understand can u post the any example of servlet context,servlet config using init,service and destroy method

  24. ashutosh says:

    very clear and very good thanks

  25. Dnyanesh Shejul says:

    Nice nd too good

  26. Ramesh says:

    Every middle class student to understod
    Thank you Bosssssssssss

  27. Harsha says:

    One of the best website for freshers and also for experienced to refer.
    Great Job..

  28. Dhwani says:

    nice explaination…. thanks

  29. Prashant Shinde says:

    Very quick understanding, nice!!!!!!

  30. Jeeva says:

    Nice Explaination…………..

  31. sarath says:

    Awesome dude..

  32. Darshan says:

    Nicely written but if there were an example it would quite be easy.Balwan ko aur balwan karo … isse chalaaki nahi, rajneeti kehte hai

  33. margi says:

    very nice and understandable explanation

  34. Thiru says:

    Very nice

  35. imran007 says:


    1.The Object of ServletConfig is created by the web container for each servlet.
    2.The getServletConfig() method of servlet Interface is used to Create ServletConfig object.
    3.The object of ServletConfig is created at the Time of Intailization process of servlet.


    1.The Object of ServletContext is created by the web container for each Web Application.
    2.The getServletConfig() method of ServletConfig Interface is used to Create ServletContext object.
    3.3.The object of ServletContext is created at the Time of deployment of Project or web application.

  36. Priya says:

    Informative blog, your all articles are amazing . Thanks for sharing , share some more basic of java servlet.

  37. Aniket Chavan says:

    I really got clarity about this topic..

  38. Yogesh K says:

    Very well explained in simple language.πŸ‘πŸΌ


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.