Category: java
Servletconfig and servletcontext
Published on 30 Sep 2026
Explanation
ServletConfig provides configuration information specific to a particular servlet.
Code:
ServletConfig config = getServletConfig();
String value = config.getInitParameter("adminEmail");
Explanation
Servlet initialization parameters can be defined using the @WebServlet annotation.
Code:
@WebServlet(
value = "/admin",
initParams = {
@WebInitParam(
name = "adminEmail",
value = "admin@example.com"
)
}
)
Explanation
ServletContext represents the entire web application and can be shared by multiple servlets.
Code:
ServletContext context =
getServletContext();
Explanation
ServletContext can store application-wide attributes accessible to multiple servlets.
Code:
ServletContext context =
getServletContext();
context.setAttribute("appName", "Employee Portal");
Explanation
ServletConfig is servlet-specific, while ServletContext is shared across the complete web application.
Code:
ServletConfig
β
One Servlet
ServletContext
β
Entire Web Application
β
Servlet 1 + Servlet 2 + Servlet 3