File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet

 
Asim Abbasi
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between doGet() doPost() and service() methods in servlet
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API:
javax.servlet.http.HttpServlet.service()
Dispatches client requests to the protected service method.
javax.servlet.http.HttpServlet.doGet()
Called by the server (via the service method) to allow a servlet to handle a GET request.
javax.servlet.http.HttpServlet.doPost()
Called by the server (via the service method) to allow a servlet to handle a POST request.
There is more information available in the API, but essentially the HTTP protocol supports multiple ways of making requests to a server. When the servlet container receives the request, it finds out which Servlet to send it to. The servlet service() method gets called, and it looks at the request, decides what sort of request it is and calls a doXXX() method. There are more than just doGet and doPost. You can override the service() method to manage all requests at a central point, but usually you want to retain the doXXX() methods, so if you do override the service() method, you would add some specific functionality and then still call the corresponding doXXX() method.
Dave
 
Scott Duncan
Ranch Hand
Posts: 364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One additional note:
doGet is default so when calling the servlet from a hyperlink or when no action is specified in the form, this is what is called. So, I've always felt that it's better to use both and have doGet call doPost for these cases.
 
Harro deGouw
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And since 'GET' is the default protocol used when requesting a page/servlet, it can be used to determine the state of your app.
Of course you could use a fullblown MVC framework like Struts, but consider this:
doGet: generate a form, action is same servlet with method=post
doPost: process input from the form
This way it's easy to separate the two views.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic