Servlet Init Params How to get init parametrs in servlets Retreiving init params in servlets

Servlet Init Parameters

We can provide the initialistaion parameters for a servlet,insdie the web.xml file.These parameters are specific to a particular servlet and are initialised at the time application is loaded. Inside the web.xml file we declare a particular init param as shown below in the web.xml fragment :

<servlet>

<servlet-name>HelloWorld</servlet-name>

<servlet-class>HelloWorld</servlet-class>

<init-param>

<param-name>username</param-name>

<param-value>john</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>HelloWorld</servlet-name>

<url-pattern>/HelloWorld</url-pattern>

</servlet-mapping>

Here inside the servlet tag where we define the servlet-name and servletclass tags,we introduce the init-param tags which consist of param-name and param-value tags.param-name is the name of the initialistaion parameter and param-value is the value to which it has to be initialised.

Inside the servlet code we can access the init parameters from the servlet config object.Servlet config instance can be get from the inherited getServletConfig() method.

The servlet code below illusrtares the above :

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html;charset=UTF-8”);

PrintWriter out = response.getWriter();

out.println(“<html>”);

out.println(“<head>”);

out.println(“<title>Servlet HelloWorld</title>”);

out.println(“</head>”);

out.println(“<body>”);

out.println(“<h1>Servlet Initialisation parameter is ” + getServletConfig().getInitParameter(“username”) + “</h1>”);

out.println(“</body>”);

out.println(“</html>”);

}

}

Here we get the init parameter “username” declared in the web.xml file by calling the method getInitParameter(“username”) on the servlet config object.The getInitParameter() method takes the name of the initialisation parameter as string.The method getInitParameterNames() returns the enumeration of parameter values,all of which are declared in the web.xml file.Having run the above servlet code we get the output as :

Servlet Initialisation parameter is john

john is the value with which we inialised the init parameter,username in the web.xml file.

Number of View :3723

Tags:

  1. #1 written by admin October 14th, 2010 at 04:59

    servlet init param dopost, The doPost() Method. init() function, servlet’s parameters. . A Fast Introduction to Basic Servlet Programming , sample code, guides..

    RE Q
  2. #2 written by asitkr October 28th, 2010 at 02:13

    java initParam

    RE Q

SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache