Finding value of initial parameter in servlet in java

Posted by Satyapriyanayak under Others category on | Points: 40 | Views : 1232
This is a process to read value from web.xml file into the servlet.The servlet can get a value from its user without having  an html form.The value of a parameter can be fetched from web.xml file by using getInitParameter() method of servlet.The initial parameter can be defined in web.xml file by using the following tags given below as,

<servlet-name>This contains a name to refer the servlet inside this file </servlet-name>
<servlet-class>This contains class name of the servlet</servlet-class>
<param-name>This contains name of the initial parameter</param-name>
<param-value> This contains value of the initial parameter </param-value>


//using initial parameter in a servlet
import javax.servlet.*;
import javax.servlet.http.*;
public class initParamServ extends HttpServlet
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
String s1=getInitParameter("gp");
PrintWriter out=res.getWriter();
out.println("<Html><body bgcolor='CYAN'>");
out.println("<H1><font color='blue'>Today's Gold price<blink>"+s1+"</blink></font></h1>");

Storing and Compiling

Store the file inside class folder of the contex (E:\Servlet\WEB-INF\classes).
Compile the file as below
javac -cp servlet-api.jar (for tomcat 6.0)

Setting in web.xml file

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns=""



Running it in web browser

Please use Mozilla Firefox browser to blink the value because Internet Explorer do not support <blink></blink> tag property.

First start the Apache tomcat 6.0, which is a web server. Then type the below URL.


Here x is the context path location, which we have to give in the server.xml file, which is present inside Tomcat installation directory (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf)

Server.xml settings

Note: - In the below server.xml file we have to specify the context path as below.
<Context path="/x" docBase="E:\Servlet" reloadable="true" debug="0" />

server.xml file
<?xml version='1.0' encoding='utf-8'?>

<Context path="/x" docBase="E:\Servlet" reloadable="true" debug="0" />

Comments or Responses

Login to post response