Monday, May 4, 2015

Form-Data Submission Methods: GET|POST

Two request methods, GET and POST, are available for submitting form data, to be specified in the
<form>'s attribute "method=GET|POST". GET and POST performs the same basic function. That is,
gather the name-value pairs of the selected input elements, URL-encode, and pack them into a query
string. However, in a GET request, the query string is appended behind the URL, separated by a '?'.
Whereas in a POST request, the query string is kept in the request body (and not shown in the URL). The length of query string in a GET request is limited by the maximum length of URL permitted, whereas it is unlimited in a POST request. I recommend POST request for production, as it does not show the strange looking query string in the URL, even if the amount of data is limited. In this tutorial, I use GET method, so that you can inspect the query string on the URL.
To try out the POST request, modify the "form_input.html":


<form method="post" action="echo">
......
</form>
Inside the servlet, GET request is processed by the method doGet(), while POST request is processed by
the method doPost(). Since they often perform identical operations, we re-direct doPost() to
doGet() (or vice versa), as follows:
public class MyServlet extends HttpServlet {
// doGet() handles GET request
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
......
......
}
// doPost() handles POST request
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {


No comments:

Post a Comment

Contact Form

Name

Email *

Message *