Difference between ServletContext and ServletConfig

A Web application can have any number of Servlets. ServletConfig is specific to a single Servlet, where as ServletContext is common to all servlets in a Web application.

Similarities of ServletContext and ServletConfig

1.Both ServletContext and ServletConfig belong to javax.servlet package.
2.Both are required to provide Initialization Parameters to a Web application or Servlet. Initialization parameters need to be provided thru Deployment Descriptor file i..e web.xml
3.Both provide getInitParameter(“parameter-name”) method, to get Initialization parameter.

Difference between ServletContext and ServletConfig

1.Both differ in scope. ServletContext is common to entire Web Application, where as ServletConfig is specific to a servlet.
2.As shown in Servlet Life Cycle ServletConfig is received as parameter to init() method of Servlet Life Cycle. Also, getServletConfig() method can be invoked to get an object of ServletConfig.
3.getServletContext() method can be invoked, to get an object of ServletContext.
4.As shown in below examples, <context-param> element specifies ServletContext initialization parameters. But <init-param> with in <servlet> , specifies ServletConfig initialization parameters.

Why to use Servlet initialization parameters

Instead of hard-coding that parameter in java source code, declaring it in web.xml has the advantage of being able to change it from time to time.

In general the same is true for all settings, for example timeouts, database username, password, database host,etc…

Another advantage of init parameters in web.xml is, all such values are available in a single file, web.xml making maintenance easier.

ServletContext example

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
    <param-name>blogname</param-name>
    <param-value>java652.com</param-value>
</context-param>

    <servlet>
        <servlet-name>NewServlet</servlet-name>
        <servlet-class>NewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>NewServlet</servlet-name>
        <url-pattern>/NewServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;

public class NewServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            ServletContext sc = getServletContext();
            
            String nam = sc.getInitParameter("blogname");
            PrintWriter pw = response.getWriter();
            
            pw.println("Parameter Value is: "+nam);
    }
}

ServletConfig example

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>NewServlet1</servlet-name>
        <servlet-class>NewServlet1</servlet-class>
        
        <init-param>
        <param-name>myParam</param-name>
        <param-value>xyz</param-value>
        
        </init-param>
        
        <init-param>
        <param-name>myParam1</param-name>
        <param-value>abc</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>NewServlet1</servlet-name>
        <url-pattern>/NewServlet1</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

public class NewServlet1 extends HttpServlet {

	public void init(ServletConfig config)
	{
            System.out.println("Am from Init method...!");
            String param_val = config.getInitParameter("myParam");
            System.out.println("Param Val:"+param_val);

            String param_val1 = config.getInitParameter("myParam1");
            System.out.println("Param Val1:"+param_val1);

            //to get enumeration of parameters
            Enumeration enm = config.getInitParameterNames();
	}
    

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //another way of getting ServletConfig
        ServletConfig sc = getServletConfig();
    }

}

In Servlet 3.0 version Annotations can also be used in java source code to set init parameters, as shown below

@WebServlet(name = "HelloServlet" , urlPatterns = { "/HelloServlet" }
    , initParams = { @WebInitParam(name = "city" , value = "New Delhi") })

In case the value is fixed forever, and it need not be changed, declaring it in java source code, also can be an option.

You may also like to read:
Java Servlet Life Cycle
How many Filters can be used for a Servlet
How many implicit objects in JSP