File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes doGet() and doPost() when to use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "doGet() and doPost() when to use" Watch "doGet() and doPost() when to use" New topic
Author

doGet() and doPost() when to use

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

I was wondering when to use doGet() and doPost() methods? I hear doGet() is used to get information from the client/browser and doPost() is used to send information back to the broswer? is it true? if so, can someone please give me an example as where to use doGet() and where to use doPost() methods instead of just the definitions?

Thanks.


Be Humble... Be Nice.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9426
    
    2

You better pick up a book or two on Servlets and JSP. Ideal would be to use Head First JSP and Servlets.

Normally a GET request is just to get anything from a location. POST request is like changing something on a location.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Though they both are the means to send the request from client to server, there are differences.

GET Method:

  • GET method appends the name-value pairs on the address bar. Thereby it has got the limitations on the number of characters (hence the values) to be passed.
  • As it is directly making the values visible, certain sensitive information like your credit card number, password etc are not suggested to be sent this way
  • Since the complete values are available in the address bar, it can be easily bookmarked but at times it may be disadvantageous.


  • Post method:
  • It overcomes the aforementioned inconveniences caused by Get method as it sends the values inside the request body.
  • there are no limitations on the number of values to be sent across and no security threats


  • Hope this helps!


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Ram Korutla
    Ranch Hand

    Joined: Dec 24, 2007
    Posts: 80
    Just to add to Raghavan's, the "GET" action is used generally to retrieve the data from a server machine.
    Although "POST" can accomplish the above the task, it is more often used to post details to a server.. ie updating the database of the server.
    Since the information being posted by a client is generally confidential,the parameters posted from the form are made invisible in the address bar.
    Rajkumar balakrishnan
    Ranch Hand

    Joined: May 29, 2008
    Posts: 445

    Just to add to Raghavan's, the "GET" action is used generally to retrieve the data from a server machine.

    Is it right?
    And both methods were used to post the values to the server with the difference that Raghavan explained there. So if you need to pass some confidential and huge amount of data then opt for POST method. Otherwise go for GET method. Am i right Raghavan?


    Never try to be a hard-worker. Be a smart-worker.
    My Blog
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Phani is right but intern he meant to say the idempotent requests.

    It is basically the values you sent to server can be used to just retrieve the values OR to update! In the database (SQL) perspective, it can be told as Select Vs Update! From the client side the values are just sent, but what those values are used for at the server side does matter!

    You may have a look at these threads, RFC for HTTP (Safe and Idempotent) methods and an earlier discussion on the same in Ranch.

    Hope this helps!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: doGet() and doPost() when to use