Finding value of initial parameter in servlet in java

Satyapriyanayak
Posted by Satyapriyanayak under Others category on | Points: 40 | Views : 822
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,
<init-param>,<param-name>,<param-value>

<servlet>
<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>
<init-param>
<param-name>This contains name of the initial parameter</param-name>
<param-value> This contains value of the initial parameter </param-value>
</init-param>
</servlet>


Example

//using initial parameter in a servlet
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class initParamServ extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
String s1=getInitParameter("gp");
res.setContentType("text/html");
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>");
out.println("</Body></html>");
}
}

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 number.java (for tomcat 6.0)


Setting in web.xml file

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

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<servlet>
<servlet-name>initParamServ</servlet-name>
<servlet-class>initParamServ</servlet-class>
<init-param>
<param-name>gp</param-name>
<param-value>21500</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>initParamServ</servlet-name>
<url-pattern>/initParamServ</url-pattern>
</servlet-mapping>
</web-app>

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.

http://localhost:8082/x/initParamServ

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" />
</Host>
</Engine>
</Service>
</Server>

Comments or Responses

Login to post response