How to use request.getparameter?
In this section of tutorial we will explain the about the request.getparameter for the request method.
In this tutorial we will show you how can you use the getParameter() method in your JSP application with the help of a simple example. Moreover, you can use the JSP codes directly to copy and paste into your JSP application to felicitate all the services provided by the getParameter() method of the request object. The codes have been provided in the example.
getparameter is basically used for obtaining the value of the HTML form fields. Once the method is executed, it returns the string type value, which is from the specified field of an HTML form. Moreover, it takes a string type parameter which is the name of the attribute of the html from where the value has to be retrieved through the request object.
Syntax of the method:
String variableName = requset.getParameter("txtUserName");
The syntax mentioned above determines that the value of the attribute "txtUserName" in the html form field is assigning to the String type variable "variableName".
In this section, you will get the entire code of the two files used in this tutorial in order to make you understand the concept of the method of the request object completely. These are as follows:
GetParameterMethodOfRequest.html
GetParameterMethodOfRequest.jsp
The html file creates a form in which the user name and password has to be entered, which is retrieved in the JSP page with the use of getParameter() method of the request object.
Here is the complete code of GetParameterMethodOfRequest.html file:
<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>
Here is the complete JSP code of the GetParameterMethodOfRequest.jsp file:
<%@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>