servletconfig vs servletcontext
In this little blogpost, I’ll try to explain the differences between ServletConfig and ServletContext and when you can use them into a Java Web Application.
| ServletConfig | ServletContext |
| Access deploy-time servlet parameters | Access web application parameters |
| One per servlet | One per web application |
ServletConfig
ServletConfig is actually very simple. Like stated above, you can retrieve parameters for the servlet which are set at deployment time.
I’ll explain this with a very simple example.
If you have created a servlet with the name “MyServlet” you can add the following 2 variables into the servlet:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>servlets.MyServlet</servlet-class>
<init-param>
<param-name>variable1</param-name>
<param-value>I'm var1</param-value>
</init-param>
<init-param>
<param-name>variable2</param-name>
<param-value>I'm var2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
You can retrieve variable2 with the following code:
getServletConfig().getInitParameter("variable2");
Retrieve all the variables of the servlet and loop over them:
Enumeration allvars = getServletConfig().getInitParameterNames();
while (allvars.hasMoreElements()) {
String next = (String) allvars.nextElement();
out.println(next + " has the value " + getServletConfig().getInitParameter(next));
}
It’s also possible to retrieve the servlets name through getServletconfig
out.println("The name of the servlet is: " + getServletConfig().getServletName());
ServletContext
ServletContext can be used to retrieve application-wide parameters, get serverparameters and store/retrieve/remove application-wide attributes.
It is possible to call the ServletContext right on or through the ServletConfig. Both are the same so you can choose between them.
if (getServletContext().equals(getServletConfig().getServletContext())) {
out.println("both calls retrieve the same Context");
}
Just like with ServletConfig, you can set parameters in the Deployment Descriptor. They are initialized on compile time. It’s not possible to remove or add them on runtime.
<context-param>
<param-name>variable3</param-name>
<param-value>Im var 3</param-value>
</context-param>
And call them in your servlet just like the ServletConfig parameters:
getServletContext().getInitParameter("variable3")
Unlike ServletConfig, ServletContext can work with attributes. These are parameters or objects which you can set into the servlet and retrieve in every other servlet of your Web Application:
getServletContext().setAttribute("test", "I'm an application-wide String");
..
out.println(getServletContext().getAttribute("test"));
..
getServletContext().removeAttribute("test");
I’ll end this little short tutorial with an example of how you can retrieve serversettings through the Context:
out.println(getServletContext().getContextPath());
out.println(getServletContext().getMajorVersion());
out.println(getServletContext().getRealPath(""));
out.println(getServletContext().getServerInfo());
If there are any more questions, just shoot them