Hackforge Academy

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

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community