Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

doPost and doGet

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67446
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Servlets.
Posts: 1197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
My, my, aren't you a big fella. Here, have a tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic