Reading Servlet Parameters
Introduction
Servlet parameters are configuration values supplied to a servlet to control its behavior without changing the source code. These parameters are defined either:
- For a specific servlet (Servlet Initialization Parameters), or
- For the entire web application (Context Parameters)
Reading servlet parameters allows servlets to be flexible, configurable, and reusable.

Types of Servlet Parameters
Servlet parameters are broadly classified into two types:
- Servlet Initialization Parameters
- Context Initialization Parameters
1. Servlet Initialization Parameters
Meaning
Servlet initialization parameters are servlet-specific parameters.
They are accessible only to the servlet for which they are defined.
Defining Servlet Parameters (web.xml)
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.app.LoginServlet</servlet-class>
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.cj.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>dbUrl</param-name>
<param-value>jdbc:mysql://localhost:3306/appdb</param-value>
</init-param>
</servlet>
Reading Servlet Parameters using ServletConfig
public void init(ServletConfig config) {
String driver = config.getInitParameter("driver");
String url = config.getInitParameter("dbUrl");
}
Important ServletConfig Methods
getInitParameter(String name)getInitParameterNames()getServletContext()
📌 ServletConfig object is created by the container and passed to the servlet.
2. Context Initialization Parameters
Meaning
Context parameters are application-wide parameters shared by all servlets in the web application.
Defining Context Parameters (web.xml)
<context-param>
<param-name>companyName</param-name>
<param-value>ABC Technologies</param-value>
</context-param>
<context-param>
<param-name>supportEmail</param-name>
<param-value>support@abc.com</param-value>
</context-param>
Reading Context Parameters using ServletContext
ServletContext context = getServletContext();
String company = context.getInitParameter("companyName");
Important ServletContext Methods
getInitParameter(String name)getInitParameterNames()setAttribute()getAttribute()
📌 One ServletContext object exists per web application.
ServletConfig vs ServletContext (Key Difference)
| Feature | ServletConfig | ServletContext |
|---|---|---|
| Scope | Single servlet | Entire application |
| Parameters | Servlet-specific | Application-wide |
| Object Count | One per servlet | One per application |
| Access | Only that servlet | All servlets |
Reading Parameters using Annotations
Using @WebInitParam
@WebServlet(
urlPatterns = "/login",
initParams = {
@WebInitParam(name = "user", value = "admin"),
@WebInitParam(name = "password", value = "admin123")
}
)
public class LoginServlet extends HttpServlet {
public void init() {
String user = getServletConfig().getInitParameter("user");
}
}
Why Servlet Parameters Are Important
- Externalize configuration
- Improve maintainability
- Avoid hardcoding values
- Easy environment-specific changes
- Promote reusable servlets
Common Use Cases
- Database configuration
- File paths
- API keys
- Application constants
- Email and logging settings
Execution Flow (Simple)
- Container reads parameters from
web.xml/ annotations - Creates
ServletConfigandServletContextobjects - Passes parameters to servlet during
init() - Servlet reads parameters and uses them during execution
Conclusion
Reading servlet parameters is a fundamental mechanism for making servlets configurable and scalable. By using ServletConfig, a servlet can access its own initialization parameters, while ServletContext enables access to shared application-level parameters. Proper use of servlet parameters leads to cleaner code, better separation of configuration from logic, and easier maintenance of web applications.
