ServletConfig vs ServletContext

The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets. It is used for intializing purposes.

The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application. It is application scoped and thus globally accessible across the pages.


ServletConfig is implemented by the servlet container to initialize a single servlet using init(). That is, you can pass initialization parameters to the servlet using the web.xml deployment descriptor. For understanding, this is similar to a constructor in a java class.

ServletContext is implemented by the servlet container for all servlet to communicate with its servlet container, for example, to get the MIME type of a file, to get dispatch requests, or to write to a log file. That is to get detail about its execution environment. It is applicable only within a single Java Virtual Machine. If a web application is distributed between multiple JVM this will not work. For understanding, this is like a application global variable mechanism for a single web application deployed in only one JVM.

Source: http://javapapers.com/servlet/difference-between-servletconfig-and-servletcontext/


Source : Difference between ServletConfig and ServletContext in Java

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

Here's how it looks under web.xml : (Source)

<servlet>
    <servlet-name>ServletConfigTest</servlet-name>
    <servlet-class>com.stackoverflow.ServletConfigTest</servlet-class>
    <init-param>
        <param-name>topic</param-name>
        <param-value>Difference between ServletConfig and ServletContext</param-value>
    </init-param>
</servlet>

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

Here's how it looks under web.xml :

<context-param>
    <param-name>globalVariable</param-name>
    <param-value>com.stackoverflow</param-value>
</context-param>

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

Difference between ServletContext and ServletConfig in Servlets JSP in tabular format(Source)

enter image description here


That's answered in the introducory text of their javadocs.

ServletConfig javadoc:

A servlet configuration object used by a servlet container to pass information to a servlet during initialization.

ServletContext javadoc:

Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.

There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)

In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.

The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.

The javadoc also contains a list of available methods along with explanation of their use. It gives a good overview of what's all available/possible with them.