What is the difference between doGet methods,doGet()and service() used in servlet?can we use service() methods replace of doGet() and doPost()?

Showing Answers 1 - 42 of 42 Answers

chandan

  • Sep 22nd, 2007
 

Yes we can. doGet is a sub class while service is a super class. doGet overrides the service method.

See the doGet() and doPost() methods are specefically meant for a servlet which extends HttpServlet which in itself extends GenericServlet class.

So whenever a request comes up firstly it goes to service() method which redirects it to doGet)( or doPost() method depending on the request i.e if it is get type request it will go to doGet() and if its kinda POST request it will go to doPost().

Dhruv

  Was this answer useful?  Yes

See, any sub class of javax.servet.Servlet is considered as servlet, this Servlet Interface contains 3 life cycle methods (ofcourse 2 more methods r there) namely init(),service(),destroy().
API providing built in abstract class javax.servlet.GenericServlet with one abstract method service(), it provides basic implementation of all other methods. GenericServlet is protocol
independent, you can use these for your custom protocol also,  now comming to javax.servlet.http.HttpServlet. (which extends GenericServlet) this is for only http protocol, in this class service() method is implemented, internally service method will call doGet()/doPost()/doTrace()/doDelete()/doPut() etc. dependent on the http method.
coming to difference between doGet() and doPost... we should say it as diff b/w http get and post methods. the major difference is about security..

in
GET,
the param values (in a web form) will be appended at the address bar in browser, hence not secure
limit data can be sent through get,
it is idempotent,
this the default method (for html form)
efficient comparatively other methods like post.

whereas in POST
This is secure as it will not show the param values on address bar of browser
we can sent huge data through post method
it is non idempotent

initially every request reaches to service() method and depends on http method,
service method calls doGet()/doPost() etc...

while desiging html page we are mentioning the method type whether it is a post are get.
 
    If we are using get method then we can override doget() method
    If we are using post method then we can override dopost() method

if we don't know which method we are using while designing a HTML page then we need to use service() method.



Question not clearely defined,
Question ) What is difference b/w doGet() and doPost() method and can we override service() method by using doGet() and doPost() methods? 

Ans ) 1.by using doGet() method we can transport limited data( 255 chars), where by using doPost() method we can transfer unimited data.

2 ) the main difference b/n is that when you select GET as method in your form the data you pass from that JSP, will be appended to the URL with your servlet address.,where if you select POST then, any kind of data will not be appended to the URL., it will be passed directly (but u can not see appended data on the URL)

3 ) so, Security (SSL) is more in doPost() method as compare to doGet() method .

      sure every request first reaches to service() method and depends on http method, after service method calls doGet()/doPost() etc...

  Was this answer useful?  Yes

amit_9b

  • May 2nd, 2009
 

doGet() : Used for processing http requests of type GET
doPost() : Used for processing http requests of type POST
service() : The generic method. When a servlet is invoked, the service() method is invoked. The service method detects the http-request type and delegates the call to doGet(), doPost(), doTrace(), etc

Yes, we can override service() method, but then that will result in the same code being exected for all http-request types (get, post, trace, etc). So, this is not recommended.

sasikumarnp

  • Jul 25th, 2009
 

doGet and doPost are HttpServlet methods.
Service is the life cycle method of servlet.
Container invoke only the service method.
Service method invoke either doGet method or doPost method based on the client
request.
The client make a get request to the Servlet service method invoke doGet method
in the HttpServlet, the client make a post request the service method invoke
doPost method in the HttpServlet.
When using get request the form fields are appended into the requested URL, that
is called request query string. URL length is limited, we can't upload file
using get request.
When using post request the form fields are added into request body, we can also
upload a file using post request.

  Was this answer useful?  Yes

Calling doGet() to doPost() and vise-versa is a good programming style.
When we want that the servlet should take the same action for both the get and post request then we use doGet() or doPost().

  Was this answer useful?  Yes

Delindia Fathima

  • Dec 20th, 2011
 

Yes we can override the service method in our servlet class.

If we override the service method then we lose the functionality provided by the httpServlet class and doXXX() methods will not be called automatically.In our implementation, we have to determine which method is used and call the corresponding doXXX() method.

Source: SCWCDExamStudyKit

  Was this answer useful?  Yes

kishore

  • May 9th, 2012
 

OK,but my question is... In HttpServlet class service method calls doGet() or doPost() method .Intead of overriding doGet() or doPost(),why should we not override service() method itself?????

  Was this answer useful?  Yes

sujata waghmare

  • Jan 10th, 2014
 

yes..but our functionality may be loss..that why web continer... calling services() then services two type.first one is public service() and second one protected services()...public services method convert object of servletrquest and servletrespons object convert to httpservletrequest and httpservltresponse..send a protected services method resive object of httpservletrequest and httpservletresponse..then depend on send a doget and dopost...only doget method data transfer url throw and dopost data transfer body throw this i s different between....doget(),dopost(),services()....

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions