Difference between ServletConfig and ServletContext in Java Servlet - GeeksforGeeks

20 Aug, 2019
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. But, the difference lies in the fact that information shared by ServletConfig is for a specific servlet, while information shared by ServletContext is available for all servlets in the web application.
ServletConfig:
ServletConfig is an object containing some initial parameters or configuration information created by Servlet Container and passed to the servlet during initialization.
ServletConfig is for a particular servlet, that means one should store servlet specific information in web.xml and retrieve them using this object.
Example:Suppose, one is building a job portal and desires to share different email ids (which may get change over time) to recruiter and job applicant.So, he decides to write two servlets one for handling recruiter’s request and another one for the job applicant.Where to store email-ids?Put email-id as a name-value pair for different servlet inside web.xml which can further be retrieved using getServletConfig().getInitParameter(“name”) in the servlet.
ServletContext:
ServletContext is the object created by Servlet Container to share initial parameters or configuration information to the whole application.
Example:Suppose, the name of one’s job portal is “NewWebsite.tg”. Showing the website name at the top of webpages delivered by different servlets, one needs to store the website name in every servlet inviting redundancy. Since the information shared by ServletContext can be accessed by every Servlet, it is better to go with ServletContext and retrieve the website name using getServletContext.getInitParameter(“Name”) whenever required.
Implementation of examples of ServletConfig and ServletContext is shown below.
web.xml
Servlet for recruiter
Servlet for applicant
<web-app>  <servlet>    <servlet-name>recruiter</servlet-name>    <servlet-class>Recruiter</servlet-class>    <init-param>      <param-name>Email</param-name>      <param-value>forRecruiter@xyz.com</param-value>    </init-param>  </servlet>    <servlet-mapping>    <servlet-name>recruiter</servlet-name>    <url-pattern>/servlet1</url-pattern>  </servlet-mapping>    <servlet>    <servlet-name>applicant</servlet-name>    <servlet-class>Applicant</servlet-class>    <init-param>      <param-name>Email</param-name>      <param-value>forApplicant@xyz.com</param-value>    </init-param>  </servlet>    <servlet-mapping>    <servlet-name>applicant</servlet-name>    <url-pattern>/servlet2</url-pattern>  </servlet-mapping>    <context-param>    <param-name>Website-name</param-name>    <param-value>NewWebsite.tg</param-value>  </context-param>  </web-app>
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;  public class Recruiter extends HttpServlet {      protected void doGet(HttpServletRequest request,                         HttpServletResponse response)        throws ServletException, IOException    {        String email            = getServletConfig()                  .getInitParameter("Email");        String website            = getServletContext()                  .getInitParameter("Website-name");        PrintWriter out = response.getWriter();        out.println("<center><h1>" + website                    + "</h1></center><br><p>Contact us:"                    + email);    }}
import java.io.*;import javax.servlet.ServletException;import javax.servlet.http.*;  public class Applicant extends HttpServlet {      protected void doGet(HttpServletRequest request,                         HttpServletResponse response)        throws ServletException, IOException    {          String email            = getServletConfig()                  .getInitParameter("Email");        String website            = getServletContext()                  .getInitParameter("Website-name");        PrintWriter out = response.getWriter();        out.println("<center><h1>" + website                    + "</h1></center><br><p>Contact us:"                    + email);    }}
Output: Deploy the app and open urls on localhost:/servlet1:/servlet2:
/servlet1:/servlet2:
/servlet1:
/servlet2:
As shown above, different servlets get different email-id with same parameter name “Email” because of different values attached to it in the web.xml.Each servlet has got their own <init-param> inside <servlet> containing email-id.
Both servlets got the access of ServletContext parameter “Website-name”.Since, <context-param> is outside of <servlet> in web.xml, hence it is independent of servlet and accessible from whole app.
Below is the table of comparison between the two:
java-servlet
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between IPv4 and IPv6
Difference Between Method Overloading and Method Overriding in Java
Difference between Process and Thread
Difference between Clustered and Non-clustered index
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples