Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doPost vs doGet

 
Reda Mokrane
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused on the difference between doPost and doGet. Can
someone explain when to use one vs. the other?
doPost and doGet has nothing to do with the "GET" and "POST" of
FORM submit in HTML is this correct??.
Regards,
Reda
 
Rama Raghavan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When a form method is GET, then doGet is invoked by the container, and if it is POST, then doPost.
Per my understanding, say, if the form method is GET and doGet() is not provided, then the request will fail.
So you would be better off providing a
public void doGet() { doPost(); }
so a request could be processed by either methods on the FORM.
 
Gurpreet Aulakh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
REDA. You are not correct. When a user submits the form the names and values of the various controls are sent in the form of a string like this Name1=Value1&Name2=Value2&Name3=Value3... ect.
Now if you use the form method GET (the default) this data string is sent APPENDED to the URL.
When you specify METHOD=POST the Request headers and a blank line are sent first then the data string is sent.
Basically GET = before Request headers
Post = after Request headers and a blank line
Gurpreet Aulakh
Sun Certified Java Developer
Sun Certified Java Programmer

[This message has been edited by Gurpreet Aulakh (edited September 06, 2001).]
 
Shamaila Mahmood
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get is the default method of form submission. A browser generates GET requeest when user
Types a URL on address bar
Follows a link
Submits a form having method=GET
Submit a form that does not specify a method
POST is generated when the user
submits a form having method=POST
doGet method is called when you submit a form having method=GET. doPost is called when you submit a form having method=POST then
Difference between them are
with get method, the query string is passed along with the request URL of the page so one can see the query string at the address bar of the browser. While in Post the query string is passed at the end of request header. So the request parameters can not be seen at the address or status bar. So post is secure while taking password or Credit card information
As get method passes form input elements with its URL so there is a limited amount of information that can ne passed with this method. The amount of information depends upon the browser. But with Post method you can send as much amount as you want.
 
Rama Raghavan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gurpreet Aulakh - Can you be more specific on what part is not correct.
You tried to explain on what is GET and POST, and I seem to have posted on what happens when GET and POST are used.
Again - IMHO -
GET sends parameters on the request header (and visible on the URL). Also referred as Query String. There is a limitation on the size of the Query String.
POST sends parameters in the body of the request. Large objects/data can be passed this way.
Now - based on form method, a doGet() or doPost() gets invoked on the servlet (assuming you are using this..)
No matter what your form method is, Request parameters (data) can be retrieved in doGet() or doPost().
If parameters is a QueryString use getParameter
If it was sent in the body, you may use getParameter or getInputStream. Just don't mix 'em(ie. don't use both ways).
Pl correct me if I am wrong
 
Gurpreet Aulakh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rama,
I was not referring to what you said. I guess I had a cached copy of page before you submitted your reply. I was referring to
what Reda said 'doPost and doGet has nothing to do with the "GET" and "POST"'
Thanks,
Gurpreet Aulakh
Sun Certified Java Developer
 
Reda Mokrane
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx you for ur explanation,
here is what I understand :
doGet is called in response to an HTTP GET request ex:
<form name=sample action="url" method=get> or from a URL

doPost is called in response to an HTTP POST request ex:
<form name=sample action="url" method=post>

but how about GET vs POST is it only about The amount of information we can send ?
Thanks,
Reda Mokrane
 
deepak adlakha
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two points between GET and POST
01) Amount of information u can get.
02) Post is secure for getting Passwords and credit cards numbers.
GET puts the form values into the URL string. GET is limited to about 256 characters (usually a browser limitation) and creates really ugly URLs.
POST allows you to have extremely dense forms and pass that to the server without clutter or limitation in size. e.g. you obviously can't send a file from the client to the server via GET.
Below lines are from http://www.microsoft.com/windows2000/techinfo/reskit/en/iisbook/c06_the_difference_between_get_and_post.htm
QUOTE
The GET method, which was used in the example earlier, appends name/value pairs to the URL. Unfortunately, the length of a URL is limited, so this method only works if there are only a few parameters. The URL could be truncated if the form uses a large number of parameters, or if the parameters contain large amounts of data. Also, parameters passed on the URL are visible in the address field of the browser�not the best place for a password to be displayed.
The alternative to the GET method is the POST method. This method packages the name/value pairs inside the body of the HTTP request, which makes for a cleaner URL and imposes no size limitations on the form�s output. It is also more secure.
UNQUOTE
Hope abv will help.
 
Gurpreet Aulakh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the +/- of using GET.
It's simple to use and debug. You don't actually have to create a form you can just enter in a URL with the proper data attached.
GET requests have limits on the amount of data that you can send.
Also, because browsers show the URL with the attahed data this makes GET inappropriate for sending data that you want to be kept secret (like a password)
POST has no limit on the amount of data you can send and because the data does not show up on the URL you can send passwords. But this does not mean that POST is truly secure. For real security you have to look into encryption which is an entirely different topic
 
deepak adlakha
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Now seen all above discussion,I got confused in how HTTP POST Request sent parameters to the server.some of u r saying part of header n some of r saying part of body n some r after header.
Request pls clarify abv n let's all argee to one.
[This message has been edited by deepak adlakha (edited September 06, 2001).]
 
Jacob Martin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the best way to learn anything is to practice by writing code.
It helped me a lot.
All the best
jacob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic