Knowledgebase: Java/JSP Hosting Plans
Getting and Setting Initialization Parameters for a Servlet
Posted by Customer Service on 25 May 2005 12:40 AM
The servlet container supports the ability to store startup and configuration information for a servlet. After the container instantiates the servlet, it makes this information available to the servlet instance. This example demonstrates a servlet that retrieves some initialization parameters in its init() method:
    // This method is called by the servlet container just before this servlet
// is put into service.
public void init() throws ServletException {
getServletContext().log("getinit init");
// Get the value of an initialization parameter
String value = getServletConfig().getInitParameter("param1");

// Get all available intialization parameters
java.util.Enumeration enum = getServletConfig().getInitParameterNames();
for (; enum.hasMoreElements(); ) {
// Get the name of the init parameter
String name = (String)enum.nextElement();

// Get the value of the init parameter
value = getServletConfig().getInitParameter(name);

// The int parameters can also be retrieved using the servlet context
value = getServletContext().getInitParameter("param1");
The initialization parameters for the servlet are specified in the deployment descriptor (i.e., web.xml file). Here is an example of a deployment descriptor that specifies two initialization parameters:

<param-name> param1 </param-name>
<param-value> value1 </param-value>
<param-name> param2 </param-name>
<param-value> value2 </param-value>
Note that the leading and trailing space around the parameter value is trimmed.
(613 vote(s))
Not helpful

Comments (0)