Difference between ServletConfig and ServletContext in Servlet

ServletConfig and ServletContext, both are objects created at the time of servlet initialization and used to provide some initial parameters or configuration information to the servlet.

ServletConfig :-

  • ServletConfig is a name value pair that store servlet specific information inside web.xml file so it has servlet wide scope.
  • This object represent a single servlet.
  • From getServletConfig() method we get the config object.
  • Its like local parameter connect with particular servlet.
  • Example online shopping of a user is a specific to single user so thats why here we can use servlet config class.

ServletContext :-

  • ServletContext object is created by Servlet Container and its configuration parameter associated with whole application
  • It represent whole web application running on particular JVM and use by all the servlet.
  • ServletContext has application wide scope so we define servlet tag in web.xml file which is outside..
  • From getServletContext() method we get the context object.
  • Example to retrieve the website name using getServletContext.getInitParameter(“Name”) whenever required for all servlets.