What is Init parameter?

Init parameter is used to pass parameter to the servlet.This is only for one servlet. Can get it from the ServletConfig object. Once you have ServletConfig reference you can call the init parameters. So you can not call the init parameters until the servlet is initialized. Because when the container initializes the servlet then it makes the 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 servlets’s init() method.
The servlet init parameters are read only once when the container initializes the servlet. At that time it reads the init params and creates a name/value pair for ServletConfig. After that the container never reads the init parameters again until unless you redeploy the servlet.

In the web.xml:
<servlet>
<servlet-name>…..</servlet-name>
<servlet-class>…….</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>bhabanipattanayak</param-value>
</init-param>
<init-param>
<param-name>city</param-name>
<param-value>bangalore</param-value>
</init-param>
</servlet>

In the Servlet code:
String name=getServletConfig().getInitParameter(“name”);
String city=getServletConfig().getInitParameter(“city”);
We can get all the init params at a time also.
Enumeration e=getServletConfig().getInitParameterNames();

No comments:

Post a Comment