Step 3: Servlet creation

Learn with our youtube video –

Ways to create Servlet-

ApproachProsConsUse Case
Implementing Servlet InterfaceProvides the most flexibility, as the developer is not constrained by the methods provided by the GenericServlet or HttpServlet classes.The developer must implement all of the methods specified in the Servlet interface, which can be tedious.When you want to handle non-HTTP request.
Extending GenericServlet Abstract ClassProvides a skeletal implementation of the Servlet interface, so the developer only needs to implement the methods they are interested in.The developer is still required to implement some methods specified in the Servlet interface.When you want to handle non-HTTP request and want to use some basic functionality provided by the GenericServlet class.
Extending HttpServlet Abstract ClassProvides a skeletal implementation of the Servlet interface specifically for HTTP requests. Handles many common HTTP-related tasks.The developer is constrained to creating servlets that handle only HTTP requests.When you want to handle HTTP requests and you are only interested in handling HTTP requests with the functionality provided by HttpServlet.

1- implement Servlet interface-
Servlet interface has total 5 methods
-3 are life cycle methods
(i) init()
(ii) service()
(iii) destroy()

2 are non- life cycle methods-
(i) getServletConfig
(ii) getServletInfo

2- extend GenericSevlet abstract class-

i– it’s a abstract class,
it implements servlet interface,
it has abstract service method

ii– it’s protocol independent.

iii– it belongs to javax.servlet package.

3- extend HttpServlet abstract class-

i– extends genericSerrvlet class and provides implementation of service methods

ii– No abstract methods,
but assigned as abstract so user can’t create object and should be created by server.

iii– it belongs to javax.servlet.http package

Best way to create servlets-> 3- extend HttpServlet abstract class-