AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-servlet: What is the difference between doGet() and doPost() ?

Where and why should we use these methods? Please explain clearly.

java x 211
servlet x 20
Posted On : 2014-04-04 23:09:51.0
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939909
up-rate
5
down-rate

Answers


These methods are called by protected service() which is one of life cycle method of servlet. It checks the request´s type. If the type of request is get it calls doGet(). and if request´s type is post then it calls doPost().
Now let´s have a look on internal code:

protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
doGet(req, resp);
}
....
//rest of the code
}
}

doGet():
-------------
We use it for static contents, When we 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 use it for dynamic contents, When we 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.

doPost() shall be used when comparatively large amount of sensitive data has to be sent.

Note: If in a form tag method attribute is set to post, the request will be processed by doPost(), by default it is processed by doGet().

Posted On : 2014-04-04 23:58:16
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in