File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes doPost and doGet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "doPost and doGet" Watch "doPost and doGet" New topic

doPost and doGet

kiran babu ssr babu

Joined: May 04, 2005
Posts: 3
I have basic question in servlet.
when to use doGet and doPost methods and in some examples i have seen that in doPost method, just doGet method is called and vice versa.
I know that when we go for doGet we could see the data which we send in the url and there is limit in it.can any one please explain me in brief about this.Thanks in advance
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Moved to Servlets.

[Asking smart questions] [About Bear] [Books by Bear]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1044
doPost() and doGet() get called by the application server when a client accesses the servlet with POST and GET methods, respectively.

IN GENERAL, GET and POST requests fulfill the same basic requirement, which is the submission of form data. That's why many servlets just have one of these methods do any real processing while the second one just forwards all requests to the first.

What are the advantages of the two HTTP requests?
- You can simulate the submission of an HTML form that uses GET by typing the form information in on the browser's addess line.
- A form that uses a POST method can include more data because it's not limited to the max length of a URL.

Is it possible to have two completely different implementation for doGet() and doPost()? Yes. You might require relatively sensitive information to be submitted with a POST so that the data isn't stored in the browsers URL history. In that case you'd implement doPost() but perhaps have doGet() return an error page.

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Some differences:

1. Acessing a web site by clicking a link or typing the address in the browser address field will almost always cause your browser to use GET.

2. Some browsers cannot handle query strings longer than 255 bytes unless you use POST.

3. You cannot upload binary data, such as a jar file containing bytecode, with GET.

4. Your URL cache (the drop-down box under the address field) and your bookmarks save the query string with the URL. This lets you save a particular inquiry, logon, etc., to a web site but it only works with GET.

5. You cannot use POST to directly access an html document.

Mike Gershman
SCJP 1.4, SCWCD in process
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The designers of HTTP intended the commands to have meaning. GET should retrieve a resource (page, document, image, etc) and not change state. POST should submit an update. PUT should put a resource. DELETE should delete it. If you want to dig deep into this, see Fielding's dissertation which defines "rest" architecture. Also google for "rest vs soap" or "rest vs rpc" for some interesting discussions.

I'm a fan of POST-Redirect-GET when doing updates. The POST updates the resource and redirects the browser to the page to GET the resource.

If a resource is available via GET then users can bookmark it, send the link to their friends or hit the refresh button without that annoying popup that says the browser must re-send request data to get the page.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: doPost and doGet
jQuery in Action, 3rd edition