getParameter() method of the Request Object gets value of the HTML form fields. These fields are linked with the form of a HTTP request. getParameter() method is used in Servlet and in JSP. In JSP request is the implicit object and can be used directly to get the parameter's value. In Servlet, it can be used by creating a reference variable of ServletRequest or HttpServletRequest interface. This method returns a string type value if requested parameter exists or returns null if they don't. Here we are discussing its use in a JSP application with examples.
Syntax:
String variable Name = request.getParameter("id");
Above syntax determines that the value of the attribute "id" of the form is assigning to the String type variable "Name".
Code of GetParameterMethodOfRequest.html:
This code creates a form in which one has to enter the username and password, which are then retrieved using getparameter() method.
<html>
<head><title>getParameter() method of request object.</title></head>
<body>
<form action="GetParameterMethodOfRequest.jsp" method="post">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>User Name: </td>
<td><input type="text" size="20" name="txtUserName" />
</tr>
<tr>
<td>Password: </td>
<td><input type="password" size="20" name="txtPassword" />
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" name="B1" /></td>
</tr>
</table>
</form>
</body>
</html>
Code of the GetParameterMethodOfRequest.jsp:
<%@page import="java.util.*" %><%
String username, password;
if(request.getParameter("txtUserName") == null)
username = "";
else
username = request.getParameter("txtUserName");
if(request.getParameter("txtPassword") == null)
password = "";
else
password = request.getParameter("txtPassword");
%>
<table align="center" bgcolor="ffff00" border="1" cellspacing=
"0" cellpadding="0">
<tr>
<td align><b>Your User Name: </b></td>
<td><%=username %><br/></td>
</tr>
<tr>
<td><b>Your Password: </b></td>
<td><%=password %></td>
</tr>
</table>
Resources: