Difference between ServletConfig and ServletContext

ServletConfig and ServletContext are the two import interfaces in ServletAPI, Lets see whats the difference between them and how to use.

ServletConfig

  • ServletConfig is available in the javax.servlet.* package.
  • ServletConfig object will be created during the initialization(init()) of the servlet.

  • Config is available only for that particular servlet.
  • ServletConfig object is created the moment we give the first request.
  • getServletConfig() method is used to get the config object.

Code Snippet

<servlet>
 <servlet-name>TestServlet</servlet-name>
 <servlet-class>com.javainterviewpoint.TestServlet</servlet-class>
 <init-param>
 <param-name>name</param-name>
 <param-value>JavaInterviewPoint</param-value>
 </init-param>
</servlet>

ServletContext

  • ServletContext is also available in the javax.servlet.* package.
  • ServletContext object will be created during the deployment of the web application.
  • Context is available for the entire web application.
  • ServletContext object is created well even before we give the first request.
  • getServletContext () method is used to get the context object.

Code Snippet

<context-param>
 <param-name>name</param-name>
 <param-value>JavaInterviewPoint</param-value>
</context-param>

Leave a Reply

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