Difference between ServletConfig and ServletContext

Difference Between ServletConfig and ServletContext in Tabular Form

ServletConfig and ServletContext Difference




ServletConfig and ServletContext both are import interfaces in ServletAPI.The Key Difference between ServletConfig and ServletContext is that ServletConfig is used by only a single servlet to get configuration information whereas ServletContext is used by multiple objects to get configuration information.

ServletConfig vs ServletContext
ServletConfig vs ServletContext

ServletConfig vs ServletContext Comparison Chart

ServletConfig ServletContext
ServletConfig object is one per servlet class. ServletContext object is global to the entire web application.
Object of ServletConfig will be created during the initialization process of the servlet. Object of ServletContext will be created at the time of web application deployment
We have to give the request explicitly in order to create the ServletConfig object for the first time ServletContext object can be available even before giving the first request
Scope: As long as a servlet is executing, the ServletConfig object will be available, it will be destroyed once the servlet execution is completed Scope: As long as a web application is executing, the ServletContext object will be available, and it will be destroyed once the application is removed from the server
ServletConfig object is used while only one servlet requires information shared by it. ServletContext object is used while application requires information shared by it
getServletConfig() method is used to obtain Servletconfig object getServletContext() method is used to obtain ServletContext object
In web.xml — <init-param> tag will be appear under <servlet-class> tag. In web.xml — <context-param> tag will be appear under <web-app> tag.




ServletConfig

  • ServletConfig available in javax.servlet.*; package
  • ServletConfig is used to get configuration information from the web.xml file.
  • If configuration information is modified from the web.xml file no need to change the servlet.
  • Example: String str = config.getInitParameter(“name”)

ServletConfig Advantage

  • ServletConfig is that you don’t need to edit the servlet file if the information is modified from the web.xml file.

ServletContext

  • ServletContext available in javax.servlet.*; package
  • ServletContext is created by the web container at the time of deploying the project.
  • It can be used to get configuration information from a web.xml file.
  • There is only one object to the entire web application.

ServletContext Advantage

  • Easy to maintain

More Difference