Why HttpServlet classs is declared as abstract class BUT with 100 % concrete functionality ?

  • It is abstract because the implementations of key servicing methods have to be provided by (i.e., overridden by) servlet developer. Since it's abstract, it's instance cannot be created.

A subclass of HttpServlet must override at least one method, usually one of these:

  • doGet, if the servlet supports HTTP GET requests.

  • doPost, for HTTP POST requests.

  • doPut, for HTTP PUT requests.

  • doDelete, for HTTP DELETE requests.

  • init and destroy, to manage resources that are held for the life of the servlet.

What happens if you dont override the methods?

  • If you extend the class without overriding any any of these methods as per your requirement, you will get a useless servlet; (i.e. it will give an error response for all requests).(HTTP 405 : Method not implemented). So, if the class was not abstract, then any direct instance of HttpServlet would be useless.

But why is it abstract?

  • So the reason for making the HttpServlet class abstract is to prevent a programming error.

  • As a servlet developer, you can choose to override the functionality of your requirement (eg : doPost) & ignore other methods.

Last updated