Fetching value of form field using servlet in java

Satyapriyanayak
Posted by Satyapriyanayak under Others category on | Points: 40 | Views : 1792
A servlet can fetch value of a form field by using getParameter() method of the servlet request.The html form can submit value of the form fields by using url pattern of the servlet into the action attribute of form tag.If the method attribute contains Get as its value and destination servlet is a sub class of Http servlet then doGet() method gets invoked. If the method attribute contains post as its value and destination servlet is a sub class of http servlet then doPost() method of the servlet gets invoked.If the destination servlet is a sub class of Generic servlet then the service() method gets invoked without depending upon value of method attribute.The getParameter() method of request accepts name of the form field as its arguments and returns value of the form fields as a string .If the specified form field doesnot exist in the request then the getParameter() method returns Null.If the form exits in a servlet and it required submit the form into the current servlet then the action attribute need not to be used in the form tag.

Example: -

A servlet to receive two numbers and display the addition.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class number extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
String s1=req.getParameter("t1");
String s2=req.getParameter("t2");
PrintWriter out=res.getWriter();
out.println("<html><body>");
if(s1 ==null)
{
out.println("<form>");
out.println("<h1>Enter No1<input type='text' name='t1'></h1>");
out.println("<input type='submit' value='submit'>");
out.println("</form>");
}
else if(s2 ==null)
{
out.println("<form>");
out.println("<h1>Enter No2<input type='text' name='t2'></h1>");
out.println("<input type='hidden' name='t1' value='"+s1+"'>");
out.println("<input type='submit' value='submit'>");
out.println("<form>");
}
else
{
int x=Integer.parseInt(s1);
int y=Integer.parseInt(s2);
out.println("<h1>Result is "+(x+y)+"</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>number</servlet-name>
<servlet-class>number</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>number</servlet-name>
<url-pattern>/number</url-pattern>
</servlet-mapping>

</web-app>

Running it in web browser

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

http://localhost:8082/x/number

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