Get all updates of Java4s
Follow Us: Email Newsletter

Difference between ServletConfig and ServletContext in Java

Servlets » On Feb 2, 2013 { 32 Comments } By Sivateja

Let us see the main differences between ServletConfig and ServletContext, which is very popular interview question as well :-)

ServletConfig

  • 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

  • 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 Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

32 Responses to “Difference between ServletConfig and ServletContext in Java”
  1. balakrishna says:

    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:

    plz…plz…!
    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…………..

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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