request.getParameter

In this section you will learn about the request.getParameter. This section will describe you about the getParameter method of request object.

request.getParameter

request.getParameter

In this section you will learn about the request.getParameter.

This section will describe you about the getParameter method of request object. getParameter() method is used to get the parameter values associated with request object of HTML form fields. These fields values are associated to HTTP request after submitting the form. This method returns the String value if the requested parameter is exist or returns null if the requested parameter doesn't exist. This method can be used in Servlet and/or in JSP. However, request is the implicit object in JSP and it can be used directly to get the parameter's value whereas, in Servlet you can use this method by creating a reference variable of ServletRequest or HttpServletRequest interface because this method is declared in the ServletRequest interface.

Syntax :

java.lang.String getParameter(java.lang.String name)

This method has one parameter

  • String name : Value of this method specified the parameter name i.e. the name of the form fileds.

Example

Here I am giving example which will demonstrate you about how this method may be used or how you can get the value of the form field from request object in a web application. We will see the same example in two ways. In the first way I will use Servlet and in the second way I will use JSP. In the Sevlet example I will use the HttpServletRequest reference to get the value of the form field using getParameter() method. HttpServletRequest is an interface and it extends the ServletRequest interface that's why using the concept of inheritance we can use the getParameter() method using reference of HttpServletRequest object. But, in the JSP example I will use the 'request' as an implicit object to get the form field values.

request.getParameter() using Servlet

input.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Input Form</title>
</head>
<body>
<form action="parameter" method="get">
<table>
<tr>
<td>Enter Your Name : </td>
<td><input type="text" name="nm"/></td>
</tr>
<tr>
<td>Ente your password :</td>
<td><input type="password" name="psw"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>

ServletGetParameter.java

package roseindia.net;

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 ServletGetParameter extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String name = request.getParameter("nm");
		out.println("Welcome, "+name);
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>getParameter</display-name>
<welcome-file-list>
<welcome-file>input.html</welcome-file> 
</welcome-file-list>

<servlet>
<servlet-name>ServletGetParameter</servlet-name>
<servlet-class>roseindia.net.ServletGetParameter</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletGetParameter</servlet-name>
<url-pattern>/parameter</url-pattern>
</servlet-mapping>
</web-app>

When you will execute the input.html page then the output will be as follows :

When you will click on submit button then the output will be as follows :

request.getParameter()using JSP

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
</head>
<body>
<form action="#" method="get">
<table>
<tr>
<td>Enter Your Name : </td>
<td><input type="text" name="nm"/></td>
</tr>
<tr>
<td>Ente your password :</td>
<td><input type="password" name="psw"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
<%
String name = request.getParameter("nm");
if(name != null && name != "")
{ 
out.println("Welcome, "+name);
}
%>
</body>
</html>

When you will execute the index.jsp page then the output will be as follows :

Download Source Code