Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between doGet() and DoPost()

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

I read the Head_First_Servlet_and_JSP book and it has already become my favorite book. But one thing, I feel, is not clear about is that difference between doGet() and doPost() methods.

The maps/pics/diagrams in the book raised the questions like

1. Which one we should use in which situation.
2. And which one we should use in normal circumstances.

and not properly covered.

it is shown that "Diane had to bought 3 books as she was not sent any response by server because of the poor programming"

But there are no clear answers for this question in detail.

...So pls do inform me if I am wrong in reading this book.

Thans and regards,

rahul.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rauhl,
Welcome to JavaRanch!

We're pleased to have you here with us in the servlets forum, but there
are a few rules that need to be followed, and one is that proper names are
required. Please take a look at the
JavaRanch Naming Policy and
adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here


On the servlet end, there is very little difference.

The two types of HTTP requests made by browsers are GET and POST.

If you click a hyper-link, type the address in to the browser window, or submit a form with it's "method" parameter set to "GET", you will be making a GET request to the server.
When browsers make GET requests, they add any parameters to the end of the URL. These are called query string parameters and they will show up in the address window of your browser.
Google searches use GET.

If you make a request by submitting a form with its method parameter set to "POST", then you will be making a post request.
When making a POST request, the browser will send the parameters in the body of the request. In this case they parameters are not seen by the end user.

In either case, the parameters can be read from within your servlet by using the getParameter(name) method.

Often if the servlet developer doesn't care whether the request came in as a GET or a POST type, s/he will simply have one method call the other:

You'll see this in the "Core Servlets" book you're reading.

According to the HTTP spec, the GET method should be 'safe' or 'idempotent'. In other words they shouldn't be used for any operations that would change the state of the app (lookups, or read operations only). See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

A great tool for learning this is the LiveHTTPHeaders plugin for FireFox. With it, you can see all of your request and response headers, including the type of request your browser is making to the server.
[ August 01, 2006: Message edited by: Ben Souther ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You read it with concentration, you will understand I am sure.

In short,

doGet() is supposed to get any resource from server. So it is supposed to be idempotent (but not necessary that is it idempotent).

doPost() is supposed to submit form to the server (change something to server). Thats is why diana ordered 3 books because she refreshed three times so request has been sent three times...

But if you will submit from through doGet() then also this problem might occur. It's called double submit problem.

 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The programmatic differences are pretty subtle. Essentially, if your servlet is responding to a form where there are many parameters enter by the client, use doPost(). If you have 1-5 parameters to be passed, feel free to use the doGet(). Be aware that when you use doGet(), your parameters will show up in the URL.
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The programmatic differences are pretty subtle. Essentially, if your servlet is responding to a form where there are many parameters enter by the client, use doPost(). If you have 1-5 parameters to be passed, feel free to use the doGet(). Be aware that when you use doGet(), your parameters will show up in the URL.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
[qb]You read it with concentration, you will understand I am sure.

In short,

doGet() is supposed to get any resource from server. So it is supposed to be idempotent (but not necessary that is it idempotent).

doPost() is supposed to submit form to the server (change something to server).

change something to server?? means what?
don't you think doGet() can also do what doPost() can? as far as it contains within the limit...taht is, it can also change the data which is on the server already...

please do not mind if i ask you some foolish doubts..


Thats is why diana ordered 3 books because she refreshed three times so request has been sent three times...

But if you will submit from through doGet() then also this problem might occur. It's called double submit problem.
[ August 01, 2006: Message edited by: rauhl ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rauhl,

Please see my earlier post in this thread regarding your screen name.
Accounts with invalid names are subject to being closed.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darryl Failla:
The programmatic differences are pretty subtle. Essentially, if your servlet is responding to a form where there are many parameters enter by the client, use doPost(). If you have 1-5 parameters to be passed, feel free to use the doGet(). Be aware that when you use doGet(), your parameters will show up in the URL.


I think, this is not the criterion to decide which one to use.

If request is going to change something in server (here I mean, updating database or model or anything) then use doPost() regardless of the number of paramaters...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read up on REST architecture at WikiPedia or Roy Fielding's PHD Dissertation. Fielding, a lead architect on HTTP, explains the intended semantics of GET, POST and other HTTP commands.

Not everybody follows this style in web applications, so REST is not hard rules or even terribly common conventions. But it's worth knowing about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic