Reading Initialization Parameters

Estudies4you
Reading Initialization Parameters
Reading Initialization Parameters
Example 4
A part from Request Parameters, we can also pass parameters using deployment descriptor file web.xml. These are called as Init Parameters and these available/scoped with in that Servlet alone and we can retrieve them using init() method so the reason they are called as Init Parameters.
  • Create following files in a folder (Please check next slide for code)
    • Java File ReadInitParamsServlet.java Contains logic to get the params details.
    • DD web.xml Deployment Descriptor
  • Compile the Java File as you did in earlier example
  • Create below structure with files that you created above
    • Folder "ReadParamsServlet" contains
    • I__"WEB-INF" Folder
    • I__"WEB-INF" Folder contains "classes" Folder and web.xml File
    • I­­__"classes" Folder contains "ReadInitParamsServlet.class" File
  • Deploy the ReadInitParamsServlet folder inside webapps folder of Tomcat
  • Bounce the Server
  • Send request using "http://localhost:8550/ReadInitParamsServlet/getDetails"
ReadInitParamsServlet.java : This prepares the response as HTML file that display init request parameters that are provided in servlet
import java.io.x;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ReadlnitParamsServlet extends HttpServlet {
private Map initParamsCollection = new HashMap();
public void init() throws ServletException {
Enumeration<String> initParams = getServletConfig().getInitParameterNames();
System.out.println(initParams + " initParams");
while (initParams.hasMoreElements())  {
String initParamName = initParams.nextElement();
String initParamValue = getServletConfig().getInitParameter(initParamName);
initParamsCollection.put(initParamName, initParamValue);
}
}
PrintWriter out = response.getWriter();
response.setContentType("text/html");
outprintln("<htmi>"); out.println("<head>");
out.println("<title>InitParams!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Init Params from Servlet!</h1>");
Iterator<Map.Entry> iter = initParamsCollection.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = iter.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
out.println("InitParameter :" +key +" Its Value:"+value+……."); 
}
out. println ("</body>");
out.println("</html>");
 out.close();
}              
}
  • web.xml
o   <servlet> tag maps your servlet class with internal name
o   <servlet-mapping> tag maps internal name with url-pattern that your invoke
<web-app>
<servlet>
<servlet-name>initparamsservlet</servlet-name>
<servlet-class>ReadInitParamsServlet</servlet-class>
<init-param>
<param-name>FirstInitParam</param-name>
<param-value>Trainer</param-value>
</init-param>
<init-param>
<param-name>SecondlnitParam</param-name>
<param-value> Learner</param-value>
</init-param>
</servlet>
<servlet-mapping>
<serviet-name>initparamsservlet</servlet-name>
<url-pattern>/getDetails</url-pattern>
</servlet-mapping>
</web-app>
Reading Initialization Parameters



To Top