Java Reference
In-Depth Information
To make a JSP page more interactive, we can read and process the data entered in
an HTML form. One way to read these values is to call the
request.getParameter
method
. This method takes a
String
parameter as input that identifies the name of
an HTML form element and returns the value entered by the user for that element on
the form. For example, if there is a textbox named
AuthorID
, then we can retrieve the
value entered in that textbox with the following scriptlet code:
String value = request.getParameter("AuthorID");
If the user leaves the field blank, then
getParameter
returns an empty string. A simple
example is given in Display 19.23. This JSP program echoes back the data entered by
the user in Display 19.18. The name of the JSP file must match the value supplied for
the
ACTION
tag of the form. In this case, the name is
EditURL.jsp
.
Display 19.23
Echoing Values Submitted by a Browser Viewing Display 19.18
This program should be saved as
EditURL.jsp
and must match
the value in the
ACTION
field of
the HTML form tag.
<html>
<title>Edit URL: Echo submitted values</title>
<body>
<h2>Edit URL>/h2>
<p>
This version of EditURL.jsp simply echoes back to the
user the values that were entered in the textboxes.
</p>
The
getParameter
method calls return as
Strings the values entered
by the user in the
URL
and
AuthorID
textboxes
from Display 19.18.
<%
String url = request.getParameter("URL");
String stringID = request.getParameter("AuthorID");
int
author_id = Integer.parseInt(stringID);
out.println("The submitted author ID is: " + author_id);
out.println("<br/>");
out.println("The submitted URL is: " + url);
%>
</body>
</html>
Sample Dialogue
Submitted on the Web browser when viewing Display 19.18.
Author ID:
2
New URL:
http://www.dansimmons.com/about/bio.htm
Web browser display after clicking Submit.
Edit URL
This version of EditURL.jsp simply echoes back to the user the
values that were entered in the textboxes.
The submitted author ID is: 2
The submitted URL is:
http://www.dansimmons.com/about/bio.htm