11

I was wondering about these servlet methods. i know something about these methods like

  • doPost has no limitations on paramater numbers while doGet has.
  • doGet is faster than doPost.
  • doPost is secured than doGet.

And my question is, as these methods takes same parameters and does the process which we implement. then what is the major difference between these methods and At which specific situation each of this method is used to process.

R9J
  • 6,605
  • 4
  • 19
  • 25

9 Answers9

14

doGet():> We will use for static contents, When we will use it, Our request parameters go through http packet header. Or size of http packet header is fixed. So only limited data can be send. or in case of doGet() request parameters are shown in address bar, Or in network data send like plane text.

doPost():>We will use for dynamic contents, When we will use it, Our request parameters go through http packet body. Or size of http packet body is not fixed. So Unlimited data can be send. or in case of doPost() request parameters are not shown in address bar, Or in network data send like encrypted text.

service():> If we will define it then we have to face server connectivity problem because its protocol independent so its not a good approach.

SatyamChaudhary
  • 381
  • 2
  • 8
7

There's the technically differences you mentioned and there's part where we're talking about REST metaphers.

Beside POST and GET, there's PUT and DELETE too. You should use GET for operations which don't alter your data, POST for creation and PUT for update of data. DELETE is obviously for deletion of data.

schlingel
  • 8,560
  • 7
  • 34
  • 62
5

doGet() and doPost() ,doPut(),doDelete() are called in different occasions with some minor differences.

Yes W3C given some specifications

GET:

A representation of the object is transferred to the client. Some URIs refer to specific variants of an object, and some refer to objects with many variants. In the latter case, the representations, encodings, and languages acceptable may be specified in the header request fields, and may affect the particular value which is returned.

POST

This method of HTTP creates a new object linked to and subordinate to the specified object. The content of the new object is enclosed as the body of the request.

And service() method receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
5

I'll focus just in the differences:

doService(): use it when you know what you're doing, the default implementation calls doGet()or doPost() so if you overwrite it, you wont get the other method called.

doGet(): by convention, a method GET shouldn't change the internal state of your application (you shouldn't do updates or the like)

doPost(): by convention, POST is used to modify the internal state of your application (do inserts, updates, deletes)

morgano
  • 17,210
  • 10
  • 45
  • 56
3

GET should be idempotent , POST may not be. GET is when you want to get something from the server , POST is to post data to the server. The GET is idempotent means the same operation applied multiple times yields the same result, where as with a POST it has side effects that results in varied outputs. You can write a non-idempotent GET , but that is against convention and you can end up with issues. GET requests can be bookmarked . POST cannot be bookmarked. Query parameters should be limited in GET , with POST you can even send files to upload.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 2
    They're not. That's just convention. – schlingel Jul 02 '13 at 06:16
  • Everything is a convention , and you should code accordingly . One should educate people to follow convention not to hack it to get themselves in trouble. – AllTooSir Jul 02 '13 at 06:20
  • @schlingel, that's why TheNewIdiot is using the words "should" and "may", he's not saying "must" – morgano Jul 02 '13 at 06:20
  • 1
    First of all: No, not everything is convention. Secondly: I wrote my comment when you didn't mentioned any convention. There were just one sentence stating that GET is idempotent POST isn't. Nevertheless: you're right, people should know conventions but they should also now that it IS a convention. – schlingel Jul 02 '13 at 06:22
2

In simple word ..get will be used when you want to fetch something from server and there is no sensitive information available in your request because its header is visible in URL. Post will be used when you are updating something in server and there is critical data e.g. passwords,account number etc. in your request. other advantage of post is its not fixed size so you can send unlimited data and data will not be visible in any case.

0

I thing you should read something about HTTP's GET and POST methods. You can start for example here. Your question is not really connected with Java and Servlets but with the very basics of web programming.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
0

I was wondering about these servlet methods. i know something about these methods like

• do Post has no limitations on paramater numbers while doGet has.

• doGet is faster than doPost.

• doPost is secured than doGet

Linga
  • 10,379
  • 10
  • 52
  • 104
raj
  • 1
0

The service method is called by the Servlet container to handle the request and reply with a proper response. Every time a request is made service method is called. We never override it as it is already been done in terms of:

  • doGet()
  • doPost()

doGet() - It requests for the information. It does not change anything in the server. doGet() method is the default HTTPServletRequest method.

doPost() - It is used to provide the information that server needs.

I just wrote down the basic difference, rest there are other performance basis differences.

myk.
  • 323
  • 1
  • 5