1

There is no any abstract methods in the HttpServlet which is declared abstract class with key word abstract. doGet and others are not abstract methods. So why HttpServlet is declared as abstract class without any abstract class?

Is the HttpServlet an example of template method pattern?

What's the template pattern and template method pattern. What's the differences?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Carl
  • 43
  • 3
  • Via documentation: "Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site.". It is abstracted because they want to force you to extend it with your own functionality. – EyeOfTheHawks Mar 23 '15 at 14:46

1 Answers1

1

HttpServlet is declared as abstract because you are meant to subclass it. By itself, it would not provide much useful behavior, so you should extend the class and define your own behavior for one or more of the methods (most commonly, either doGet or doPost).

Yes, HttpServlet is an example of the template method pattern. The doXXX methods in HttpServlet are called from the service method. In other words, we have a template method (service) deferring some of its processing to individual methods defined in subclasses. These methods are selected at compile time, so this is an example of the template method pattern.

See also:

Community
  • 1
  • 1
Andrew
  • 362
  • 3
  • 8