In Sun's j2ee tutorial, on page 240, it said "There are two ways to generate query strings: " 1. "A query string can explicitly appear in a web page" 2. "A query string is appended to a URL when a form with a GET HTTP method is submitted." What confused me is that I thought we need to use POST method to append form data to query string, and it's on page 32 of Servlet 2.3 Spec. Please help Naichen
A query String is only passed on a GET request. It is that part of the URI (URL) passed to the server starting after the Path information of the Request and preceeded with a ?. Example http://www.javaranch.com/bookpromo.jsp?name=carl&status=Sheriff This would cause a get request with the Query String of name=carl&status=Sheriff If a post is performed, the parameters are past on in the Posted Body which is a seperate Stream from the URI Request. (How they are fomatted is discussed in the HTTP standard but, a little beyound this topic) I don't see your reference on page 32 could you be a little more specific. I do see a statement on page 31 which could be confusing if read fast. "Data from the query string and the post body are aggregated into the request parameter set."
This is from Servlet Spec, "The following are the conditions that must be met before post form data will be populated to the parameter set: 1. The request is an HTTP or HTTPS request. 2. The HTTP method is POST 3. The content type is application/x-www-form-urlencoded 4. The servlet has made an initial call of any of the getParameter family of meth-ods on the request object." My question is, if we use HTTP GET method, can we post data to server? I nerver used GET method with html form. By the way, in Sun's j2ee tutorial, the explaination for the second way to generate query string is, "A query string is appended to a URL when a form with a GET HTTP method is submitted. In the Duke's Bookstore application, CashierServlet generates a form, a user name input to the form is appended to the URL that maps to ReceiptServlet, and ReceiptServlet extracts the user name using the getParameter method." I checked CashierServlet, it does use html form with POST method. please clarify. Thanks Naichen
posted 18 years ago
OK, the question should be My question is, if we use HTTP GET method, can we send data to server? The answer is yes. You append a query String to the end of the URL. As in my example above. The second way looks exactly like the html post forms you apperantly are using. The only difference is the method attribute of the tag. <form action="http://www.javaranch.com" method="GET"> <input type="text" name="name"> <input type="submit"> </form> ------------------ I Hope This Helps Carl Trusiak, SCJP2