Tuesday 3 September 2013

doPost()and doGet() methods in Servlet

GET/POST

We have seen that when the type of request is get, then service() calls the doGet(), when the type of request is post, then service() calls the doPost(). But the question is:

When do we go for doGet() and When do we go for doPost()?
Ans:  When we use doGet() method:  get HTTP request only has a request line and HTTP header where as a post has a request line HTTP and header and also has a HTTP body.
For example when you type your yahoomail and try to get your emails and you pass the username and password in yahoo Login screen so how are these username and password  passed to the server. You have seen that these are passed in the form parameters and in the form elements of the request . So when you are going for these form elements are appended to the end of the URL so whaever URL you are sending it the form parameters are appended to that URL and send it across to the server so the form parameters whatever information you are passing to the server that is visible in the URL  when you are going for a Get().

But in the case of Post(): the form parameters are passed in the HTTP body, and these HTTP body is not seen by anyone, and it maintains the secret which can not seen by anyone so the form parameters are here send in the HTTP body but not in the URL (Which is seen by everyone).

next point the size: so although you can send the information using Get() appending it to the end of the URL, the amount of data which you can send using Get() method is limited, you can send very less amount of data to implement a Get() but whereas in a Post() you can send large amount of data to the server because we send the data in the HTTP body but not on the request line.

When should we use Get() and when should we use Post()?
Ans: whenver something to fetch something from server you go for a doGet() method, and when you want to post something to the server, you go for doPost().

Example: if you want to search servlet in google search: so you go to Google .com you type "servlet" in the search text box and you type on search so the server actually gets you back with all search elements for the servlets so you are tring to fetchi something from server right? in this case you are using the doGet() method.

Now take another example: when you are filling online application form, then you will enter lots of fields and then you submit that form to the server, in this case you are actually you  are filling form and submiting the data to the server. So when you want to submiting to the server you use a Post() method.

So basic term rule is when you want to fetch something from the server go for Get() method, when you want to post something to the server go for doPost() method.

but there is one condition is there you know that using Get() you can pass some parameters to the server But if the typ of the parameters sending a sensitive the eventhough the amount of parameters we are sending less you should go for a Post() method. For Example when you are tring to get your email, passing username and password in the yahoo log on screen and click on go to my email by clickin log on button and that you will all you get your messages , your particular email address so here eventhough you are trying to get something (email) from the server (yahoo server) you should be using doPost() method because the type of information you are sending is 'sensitive' information. When you go for a get, the username and password would have been appended to the end of the URL and send it across to the server and everyone can see this username and password but you dont want anyone to see your username and password which you are using so this information is sensitive. So all sensitive transactions whaever the amount of data, whatever the type of data transaction you are doing get or fetch you should go for doPost() method because in the Post() the data is sent in the HTTP body , not be seen by anyone


                                     doGet() doPost()
HTTP request The request contains only the request line and HTTP Header Along with the      request line and header it also contains HTTP body


Parameter The form elements are passed to the server by appending at The form elements are passed to the server in the body of the HTTP request
passing the end of the URL

size The parameter data is limited (the limit depends on the container we can send large amount of data to the web server

Usage go get(fetch) information from the server to process the sent data

No comments:

Post a Comment