Main job of ServletConfig is to give you init parameters
Example for one init parameter
Step1: Insert below code in the Deployment descriptor web.xml. .Just make sure it’s inside the < servlet > element in the DD.
< servlet >
< servlet-name >ServletController< /servlet-name >
< servlet-class >com.examp.ServletController< /servlet-class >
< init-param >
< param-name >Country< /param-name >
< param-value >India< /param-value >
< /init-param >
< /servlet >
Step2: Insert the following code in the Servlet program’
PrintWriter out = response.getWriter();
out.println(getServletConfig().getInitParameter(“Country”));
Step 3: Output is given below
India
Example for more than one init parameter
Step1: Insert below code in the Deployment descriptor web.xml. Just make sure it’s inside the < servlet > element in the DD.
< servlet >
< servlet-name >ServletController< /servlet-name >
< servlet-class >com.examp.ServletController< /servlet-class >
< init-param >
< param-name >Country< /param-name >
< param-value >India< /param-value >
< /init-param >
< init-param >
< param-name >State< /param-name >
< param-value >Kerala< /param-value >
< /init-param >
< /servlet >
Step2: Insert the following code in the Servlet program
PrintWriter out = response.getWriter();
//getting parameter name
java.util.Enumeration e = getServletConfig().getInitParameterNames();
while(e.hasMoreElements()) {
String element=(String) e.nextElement();
out.println(""+element+ getServletConfig().getInitParameter(element));
out.println("< br >");
}
Step3: Output is given below
Country India
State Kerala
No comments:
Post a Comment