Saturday 30 March 2013

Retrieving the Contents of a HTML form

Forms are, of course, the most important way of getting information from the customer of a web site. In this section, we'll just create a simple color survey and print the results back to the user.
First, create the entry form. Our HTML form will send its answers to form.jsp for processing.
For this example, the name="name" and name="color" are very important. You will use these keys to extract the user's responses.
form.html
<form action="form.jsp" method="get">
<table>
<tr><td><b>Name</b>
<td><input type="text" name="name">
<tr><td><b>Favorite color</b>
<td><input type="text" name="color">
</table>
<input type="submit" value="Send">
</form>
Keeps the browser request information in the request object. The request object contains the environment variables you may be familiar with from CGI programming. For example, it has the browser type, any HTTP headers, the server name and the browser IP address.
You can get form values using request.getParameter object.
The following JSP script will extract the form values and print them right back to the user.
form.jsp
Name: <%= request.getParameter("name") %> <br>
Color: <%= request.getParameter("color") %>

Retrieving a Query String !

An include action executes the included JSP page and appends the generated output onto its own output stream. Request parameters parsed from the URL's query string are available not only to the main JSP page but to all included JSP pages as well. It is possible to temporarily override a request parameter or to temporarily introduce a new request parameter when calling a JSP page. This is done by using the jsp:param action.
In this example, param1 is specified in the query string and is automatically made available to the callee JSP page. param2 is also specified in the query string but is overridden by the caller. Notice that param2 reverts to its original value after the call. param3 is a new request parameter created by the caller. Notice that param3 is only available to the callee and when the callee returns, param3 no longer exists. Here is the caller JSP page:
If the example is called with the URL:
http://hostname.com?param1=a¶m2=b
the output would be:


No comments:

Post a Comment