ServletConfig and ServletContext are important interfaces in Java Servlets used for configuration and communication. ServletConfig is used to store servlet-specific initialization parameters, while ServletContext is used to share data across the entire application. Together, they help manage configuration and enable communication between servlets.
- Servlet Config holds configuration data specific to a single servlet and is initialized once.
- Servlet Context stores application-wide data and is shared among all servlets.
ServletConfig
ServletConfig is an object that contains initialization parameters for a specific servlet. It is created by the servlet container and passed to the servlet during initialization.
- Defined inside <servlet> in web.xml
- Each servlet has its own ServletConfig object
- Accessed using getServletConfig()
Syntax
ServletConfig config = getServletConfig();
String value = config.getInitParameter("param-name");
Example: The following example demonstrates how ServletConfig is used to provide servlet-specific initialization parameters that are accessible only to a single servlet.
web.xml
<servlet>
<servlet-name>RecruiterServlet</servlet-name>
<servlet-class>RecruiterServlet</servlet-class>
<init-param>
<param-name>Email</param-name>
<param-value>recruiter@jobportal.com</param-value>
</init-param>
</servlet>
RecruiterServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RecruiterServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
ServletConfig config = getServletConfig();
String email = config.getInitParameter("Email");
PrintWriter out = response.getWriter();
out.println("Recruiter Contact Email: " + email);
}
}
Output:
Recruiter Contact Email: recruiter@jobportal.com
Explanation:
- ServletConfig object is created per servlet
- The parameter Email is available only to RecruiterServlet
- Another servlet can have the same parameter name with a different value
ServletContext
ServletContext is an object that holds application-wide configuration information. It is shared among all servlets in the web application.
- Application-wide configuration
- Defined using <context-param> in web.xml
- Only one ServletContext object per application
- Accessed using getServletContext()
Syntax
ServletContext context = getServletContext();
String value = context.getInitParameter("param-name");
Example: The example demonstrates how ServletContext is used to share application-wide configuration data that can be accessed by all servlets.
web.xml
<context-param>
<param-name>WebsiteName</param-name>
<param-value>NewWebsite.tg</param-value>
</context-param>
AnyServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class AnyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = getServletContext();
String website = context.getInitParameter("WebsiteName");
PrintWriter out = response.getWriter();
out.println("Website Name: " + website);
}
}
Output:
Website Name: NewWebsite.tg
Explanation:
- ServletContext object is shared across the entire application
- The parameter WebsiteName is accessible by all servlets
- Defined once in web.xml, avoids duplication
ServletConfig Vs ServletContext
| ServletConfig | ServletContext |
|---|---|
| ServletConfig is servlet specific | ServletContext is for whole application |
| Parameters of servletConfig are present as name-value pair in <init-param> inside <servlet>. | Parameters of servletContext are present as name-value pair in <context-param> which is outside of <servlet> and inside <web-app> |
| ServletConfig object is obtained by getServletConfig() method. | ServletContext object is obtained by getServletContext() method. |
| Each servlet has got its own ServletConfig object. | ServletContext object is only one and used by different servlets of the application. |
| Use ServletConfig when only one servlet needs information shared by it. | Use ServletContext when whole application needs information shared by it |