Servlet is also java program but there is no main method in servlet.Who will take role of main method on servet.
Methods of Servlet interface The init, service and destroy are the life cycle methods of servlet.
When the Java interpreter executes an application (by being invoked upon the application's controlling class), it starts by calling the class's main method. The main method then calls all the other methods required to run your application.
There are two main types of Servlet. They are Generic and HTTP servlets. We can use the constructor method to initialize the Servlets with the help of init() and the destructor method to remove the servlet from the resources using destroy().
There is no "main" method (as in application entry point) in web applications, since each servlet is an independent entry point.
Servlets are designed to run inside a servlet container (eg. Apache Tomcat). Execution of a servlet happens in the following manner: The servlet container calls the GenericServlet.service()
method on a servlet which typically calls the appropriate doXxx()
method, eg. doGet()
, doPost()
, etc. The doXxx()
method is responsible for interpreting the HTTP request and serving an appropriate response. GenericServlet.service()
is roughly analagous to main()
in a plain old java class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With