- Request parameters are used by the servlet to receive data from client to process the request.
- Some specific data must be supplied to the servlet at the time of initialization of the servlet and this data is specific to the client.But data is not supplied by the client via request parameters
- Use initialization parameters to supply the data to the servlet at the time of initialization of the servlet. initialization parameters are set in deployment descriptor (Web.xml).
- Servlet can access these parameters during the run time
- Request parameters change form Request to request
- Initialization parameters change from servlet to servlet.
Configure Initialization parameters in Web.xml file:
ServletConfig Methods :
- public String getInitParameter(String name): Returns a String containing value of the named parameter, or null if the parameter does not exist.
- public Enumeration getInitParameterNames(): Returns names of the servlets initialization parameters as an enumeration of String objects, or an empty enumeration if servelt has no initialization
- public String getServletName():Returns the name of the servlet.
- public ServletContext getServletContext():Returns an object of ServletContext.
How to get Initialization parameters:
- To access initialization parameters of a servlet we use servelt config object.
- ServletConfig is an interface defined by javax.servlet package and this interface is implemented by container.
- ServletConfig is the object of the class that implements ServletConfig interface and this object is created by servlet conatainer with servlet init() method parameters.
- For each servlet One ServletConfig object is created by the container.
String s=sc.getInitParameter("email");
To get all values:
ServletConfig config=getServletConfig();
Enumeration<String> e=config.getInitParameterNames();
String names="";
while(e.hasMoreElements()){
str=e.nextElement();
out.print("<br>Name: "+str);
out.print(" value: "+config.getInitParameter(names));
}
ServletConfig config=getServletConfig();
Enumeration<String> e=config.getInitParameterNames();
String names="";
while(e.hasMoreElements()){
str=e.nextElement();
out.print("<br>Name: "+str);
out.print(" value: "+config.getInitParameter(names));
}
No comments