Notes on Form Data for Servlets
by, 11-28-2011 at 07:25 PM (828 Views)
When you look at the URL in your web browser if you are using a search engine or on a retail site, you will notice URLs http://host/path?user=James+Rogers&s...rish&sub=music. The part after the question mark (i.e., user=James+Rogers&subj= folk&sub=irish&sub=music) is known as form or query data and is the most common way to get information from a Web page to a server-side program. Form or query data can be attached to the end of the URL after a question mark, for GET requests, or sent to the server on a separate line, for POST requests. (Note that there was an urban myth of 256 characters for URIs on GET requests but this was never true and only a limitation caused by some server implementations).
With URLs such as the one shown above, extracting the needed information from this form data is using CGI programming is trying if not extremely difficult. Consider that you have to read the data one way for GET requests (through the QUERY_STRING environment variable for CGI) and for POST requests (through reading the standard input in traditional CGI). Then you have to divide up the pairs by the ampersands, then separate the parameter names (via the equal signs) from the parameter values. Finally, you have to URL-decode the values. The easy ones to deal with are alphanumeric characters as they are sent unchanged, but other characters need to be converted to various character strings. On the server-side program the process is reversed. Look at one of my previous tips on how servlets do parse query strings automatically and provide you with the parameters in a easy to use manner.