|
1. IntroductionIf you've ever used a Web search engine, visited an on-line bookstore, tracked stocks on-line, or asked a Web-based site for quotes on plane tickets, you've probably seen funny looking URLs likehttp://host/path?user=Marty+Hall&origin=bwi&dest=lax . The
part after the question mark (i.e.
user=Marty+Hall&origin=bwi&dest=lax ) is known as form
data, and is the most common way to get data from a Web page to a
server-side program. It can be attached to the end of the URL after a question
mark (as above), for GET requests, or sent to the server on a
separate line, for POST requests.
Extracting the needed information from this form data is traditionally one of
the most tedious parts of CGI programming. First of all, you have to read the
data one way for One of the nice features of Java servlets is that all of this form parsing is
handled automatically. You simply call the 2. Example: Reading Three ParametersHere's a simple example that reads parameters namedparam1 ,
param2 , and param3 , listing their values in a bulleted
list. Note that, although you are required to specify response settings (content
type, status line, other HTTP headings) before beginning to generate the
content, there is no requirement that you read the request parameters at any
particular time.
Also note you can easily make servlets that can handle both 2.1 ThreeParams.javaYou can also download the source or try it on-line. Note: also uses ServletUtilities.java, shown earlier.package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class ThreeParams extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Reading Three Request Parameters"; out.println(ServletUtilities.headWithTitle(title) + "<BODY>\n" + "<H1 ALIGN=CENTER>" + title + "</H1>\n" + "<UL>\n" + " <LI>param1: " + request.getParameter("param1") + "\n" + " <LI>param2: " + request.getParameter("param2") + "\n" + " <LI>param3: " + request.getParameter("param3") + "\n" + "</UL>\n" + "</BODY></HTML>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } 2.2 ThreeParams Output3. Example: Listing All Form DataHere's an example that looks up all the parameter names that were sent and puts them in a table. It highlights parameters that have zero values as well as ones that have multiple values. First, it looks up all the parameter names via thegetParameterNames method of HttpServletRequest . This
returns an Enumeration . Next, it loops down the
Enumeration in the standard manner, using
hasMoreElements to determine when to stop and using
nextElement to get each entry. Since nextElement
returns an Object , it casts the result to a String and
passes that to getParameterValues , yielding an array of
String s. If that array is one entry long and contains only an empty
string, then the parameter had no values, and the servlet generates an
italicized "No Value" entry. If the array is more than one entry long, then the
parameter had multiple values, and they are displayed in a bulleted list.
Otherwise the one main value is just placed into the table.
3.1 ShowParameters.javaNote: also uses ServletUtilities.java, shown earlier.package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Shows all the parameters sent to the servlet via either * GET or POST. Specially marks parameters that have no values or * multiple values. * 3.2 Front End to ShowParametersHere's an HTML form that sends a number of parameters to this servlet. Right click on the source code link to download the HTML. Left click on the link to try it out on-line. It usesPOST to send the data (as should all forms that have
PASSWORD entries), demonstrating the value of having servlets
include both a doGet and a doPost . However, just for
the sake of illustration, a version using
GET can also be downloaded or tried out on-line.
PostForm.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>A Sample FORM using POST</TITLE> </HEAD> <BODY BGCOLOR="#FDF5E6"> <H1 ALIGN="CENTER">A Sample FORM using POST</H1> <FORM ACTION="/servlet/hall.ShowParameters" METHOD="POST"> Item Number: <INPUT TYPE="TEXT" NAME="itemNum"><BR> Quantity: <INPUT TYPE="TEXT" NAME="quantity"><BR> Price Each: <INPUT TYPE="TEXT" NAME="price" VALUE="$"><BR> <HR> First Name: <INPUT TYPE="TEXT" NAME="firstName"><BR> Last Name: <INPUT TYPE="TEXT" NAME="lastName"><BR> Middle Initial: <INPUT TYPE="TEXT" NAME="initial"><BR> Shipping Address: <TEXTAREA NAME="address" ROWS=3 COLS=40></TEXTAREA><BR> Credit Card:<BR> <INPUT TYPE="RADIO" NAME="cardType" VALUE="Visa">Visa<BR> <INPUT TYPE="RADIO" NAME="cardType" VALUE="Master Card">Master Card<BR> <INPUT TYPE="RADIO" NAME="cardType" VALUE="Amex">American Express<BR> <INPUT TYPE="RADIO" NAME="cardType" VALUE="Discover">Discover<BR> <INPUT TYPE="RADIO" NAME="cardType" VALUE="Java SmartCard">Java SmartCard<BR> Credit Card Number: <INPUT TYPE="PASSWORD" NAME="cardNum"><BR> Repeat Credit Card Number: <INPUT TYPE="PASSWORD" NAME="cardNum"><BR><BR> <CENTER> <INPUT TYPE="SUBMIT" VALUE="Submit Order"> </CENTER> </FORM> </BODY> </HTML> 3.3 Submission Result
|