
Java
A Web application includes many parts. It is more than just one servlet or JSP. Numerous JSPs and one or more Servlets and other supporting Java classes together form the web application. To help manage an application, you will sometimes need to set and get information that all of the servlets share together, which we will refer to as context-wide.
For Example, if you want a single name using which you can refer to the application, you can set it in the servlet context and have it shared across all instances that use the application.
Ex Code:
public void init(ServletConfig config) throws ServletException
{
super.init(config);
// Get the Context
ServletContext context =config.getServletContext();
// Set the attribute
context.setAttribute("appName", "My Test App");
}
Any time you want, you can refer to this attribute in the context and get its value like below:
String appName = context.getAttribute("appName");
After the above line of code, the variable appName will have the value "My Test App
Copyright © 2026 eLLeNow.com All Rights Reserved.