Attributes and Listeners:
Servlet init parameter:
In order to hardcode the value in the servlet, we can put(configure) that value in the Deployment Descriptor(DD). For example
Printwriter out = response.getWriter();
Out.printl(boo@gmail.com);
Instead of hard coding like this we can mention this value in DD, by that without touching the servlet, he can change the value in DD and do redeploy.
In DD (web.xml):
<servlet>
<servlet-name>Beer</servlet-name>
<servlet-class>TestinitParam</servlet-class>
<init-param>
<param-name>adminEmail</param-name>
<param-value>boo@gmail.com</param-value>
</init-param>
</servlet>
In Servlet code:
Out.println(getServletConfig().getInitParameter(“adminEmail”));
Key Points:
- We can’t use servlet init parameter until the servlet is initialized.
- We can’t use servlet init parameter from the constructor of the servlet.
- When the container initializes a servlet, it makes a unique servletConfig for the servlet.
- The container “reads” the servlet init parameters from the DD and gives them to the ServletConfig, then passes the ServletConfig to the servlet init() method.
Two methods of init():
int(): withoutparameter.
init(servletConfig): withparameter. It will call no-arg init() method.
Steps to get the init parameter values through servletConfig:
The servlet init parameters are read only once, when the container initializes the servlet.
- Container reads the DD for the servlet, including the servlet int parameters.
- Containter creates a new servletConfig instance for this servlet..
- Container creates a name/value pair of strings for each servlet init parameter.
- Container gives the servletConfig reference to the name/value init parameters.
- Container creates a new instance of servlet class.
- Container calls the servlet’s init() method passing in the reference to the servletConfig.
Redeploy:
Redeploy means, stop and start the tomcat again. It is a bad idea to redeploy to get the changed values in DD. We can do hot-redeploy. It is done to get the changed values without restarting the server. Even hot-redeploy is also a bad idea.
Remedy:
If the values of init parameters are going to change frequently, then better off having servlet methods get values from a file or database.
Sample example:
In DD (web.xml):
<web-app ….>
<servlet>
<servlet-name>Beer</servlet-name>
<servlet-class>TestinitParam</servlet-class>
<init-param>
<param-name>adminEmail</param-name>
<param-value>boo@gmail.com</param-value>
</init-param>
<init-param>
<param-name>userEmail</param-name>
<param-value>user@gmail.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Beer</servlet-name>
<url-pattern>/Test.do</url-pattern>
</servlet-mapping>
</web-app>
In Servlet class:
Package com.example;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Public class TestInitParam extends HttpServlet{
Public void doGet(HttpServletRequest req, HttpServletResponse res) throws IoException, ServletException{
Res.setcontentType(“text/html”);
PrintWriter out = res. getWriter();
Enumeration e = getServletConfig().getInitParameterNames();
While(e.hasMoreElements){
Out.println(e.nextElement());
}
out.println(getServletConfig().getInitParameter(“userEmail”);
out.println(getServletConfig().getInitParameter(“adminEmail”);
}
}
ServletConfig is restricted to only to that Servlet. One Servlet cant access the init parameters of Other Servlet. To make the inti parameter global i.e all the servlet to access common init parameter, then we have to declare as the Context init parameter.
Context init parameter:
Context init parameters works as like servlet init parameter,except context parameter are available to the entire web application. So both JSP and Servlet have access to the context init parameters.
Sample Program:
In DD (web.xml):
<servlet>
<servlet-name>Beer</servlet-name>
<servlet-class>TestinitParam</servlet-class>
</servlet>
<context-param>
<param-name>userEmail</param-name>
<param-value>user@gmail.com</param-value>
</context-param>
In Servlet code:
Out.println(getServletContext().getInitParameter(“userEmail”));
Difference between Servlet init and Context init parameters
Attributes Context init parameters Servlet init parameters
DD <context-param> tag, is used, <inti-param> tag is used;
inside <web-app> inside the <servlet>
Servlet Code getServletContext().getinitParameter()
getServletConfig.getInitParameter()
——————————————————————————————————-
Availability to any servlet and jsp with in web app only to the Particular
Servlet for which <init-parameter is configured.
——————————————————————————————————-
servletConfig: It is one per Servlet.
ServletContext: It is one per web application.
If app is in single JVM servletContext will be one.
If app is Distributed app, the one ServletContext per JVM.
Init parameters are deploy-time constants.
We can get them at runtime, but you cant set them. There is no setInitParameter().

Hey Raj,
Excellent explanation! I read a lot of documents before, but i couldn’t found all the information like this in one place.
I appreciate your work. Keep on publishing on other technologies also(like struts, springs……….)
Thanks & Regards,
SRK.