Java

Input Parameters in a Servlet

Programmingempire

Since, a web page contains a form to get data from the users, the Input Parameters in a Servlet are received from the HTML form. Basically, an HTML form contains various input elements like textbox, checkbox, dropdownlist, radio buttons and so on. In order to pass the values entered by the user, these input elements must have the name attribute specified.

The getParameter() Method in the Servlet

Basically, this method receives input data from the HTML form. It takes a string parameter that specifies the name of the corresponding input element in the HTML form. Further, this method returns the value entered in that element. However, if value doesn’t exist, it returns null.

The getWriter() Method

In fact, this method is called using the implicit object response and returns an object of the PrintWriter class.

PrintWriter Class

Basically, this class is responsible for printing the string to a text-output stream. Further, it prints the formatted representation of objects.

An Example of Processing Input Parameters in a Servlet

The following code shows a simple HTML form that contains two text boxes to allow user to enter two numbers.

MyForm.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <form name="f1" method="POST" action="ComputeServlet">
 	Enter a number: <input type="text" name="t1"/><br/>
 	Enter another number: <input type="text" name="t2"/><br/>
 	<input type="submit" value="Compute"/>
</form>
</body>
</html>

As can be seen in the above code, the form has two input text fields and a submit button. Further, the action attribute specifies the name of servlet class that will be executed when the user submits the form.

Processing Input Parameters in a Servlet

The following code shows a servlet that accepts the input parameters and processes these parameters. As an illustration, the input values are passed as strings using the getParameter() method. Further, the input values are transformed to integers and different arithmetic operations are applied on them. Finally, the object of PrintWriter class writes the corresponding HTML to the output stream.

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet("/ComputeServlet")
public class ComputeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public ComputeServlet() {
        super();
    }
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter ob=response.getWriter();
		String s1=request.getParameter("t1");
		String s2=request.getParameter("t2");
		int x, y, z;
		x=Integer.parseInt(s1);
		y=Integer.parseInt(s2);
		String str="";
		z=x+y;
		str+="Sum = "+z+"<br/>";
		z=x-y;
		str+="Difference = "+z+"<br/>";
		z=x*y;
		str+="Product = "+z+"<br/>";
		z=x/y;
		str+="Division = "+z+"<br/>";
		
		ob.println("<html><head><title>Simple Computation</title></head>");
		ob.println("<body><center><h2>Computation Result</h2></center>");
		ob.println("<h3>");
		ob.println(str);
		ob.println("</h3></body></html>");
	}
}

Output

The HTML Form
The HTML Form

Once, the user clicks on the Compute button, the servlet executes and produces the following output.

Processing Input Parameters in a Servlet
Processing Input Parameters in a Servlet

programmingempire

You may also like...