getParameter() Method Of The Request Object
This section illustrates you about the getParameter() method of the request object. This section provides you the best illustration for why the method used or how to use in your JSP application. You can directly copy the provide JSP code (provided in the section) and paste it into your JSP application for getting all the facilities provided by the getParameter() method of the request object.
This is the method, used for getting the value of the HTML form fields. This method returns the string type value i.e. the value of the specified field of an HTML form. This method takes a string type parameter which is the name of the attribute of the html which value has to be retrieved through the request object.
Syntax of the method:
String variableName = requset.getParameter("txtUserName");
Above syntax determines that the value of the attribute "txtUserName" of the html form field is assigning to the String type variable "variableName".
This section provides the complete code of two files which have been used in the section for the complete and best illustration of the method of the request object. These are as follows:
- GetParameterMethodOfRequest.html
- GetParameterMethodOfRequest.jsp
The html file constructs a form in which you have to enter the user name and password which is retrieved in the JSP page by using the 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>
Output for the GetParameterMethodOfRequest.html file:
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>
Output for the GetParameterMethodOfRequest.jsp file: