Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I override service() or doPost()?

Tags:

java

servlets

I was reading a book on servlets, in that book a brief explanation is given about the servlet class, as well as the HttpServlet class.

There is one example for filling in a form- for that form, the servlet's doPost() method is overridden by the class. But for another example of a login form, the service() method is overridden instead.

I want to know why the 2 different approaches- I thought that usually we put our custom code into doPost() (or doGet()) and let service() remain as it is. Is there any reason behind using either one of the 2 approaches, or can I use both approaches in any situation?

like image 352
Arvind Avatar asked Jul 25 '11 20:07

Arvind


1 Answers

Do not override service() method. The preferred approach is using doPost() for post and doGet() for get. Here is an excellent post on what each does. http://www.jguru.com/faq/view.jsp?EID=47730

If you must respond to requests made by a client that is not using the HTTP protocol, you must use service().

like image 161
Amir Raminfar Avatar answered Sep 29 '22 19:09

Amir Raminfar