Programs that add functionality to a web server.
Each request to be handled by a servlet is handled by a separate Java thread within the Web server process. Hence, there are same amount of threads as requests and all these threads/requests share a single copy of the servlet class. This single copy persists between requests.
Before servlets, CGI was used. However, it was non-Java and used a different process for each server request. Hence, it was non-portable and very resource consuming. Also, it lacked other features provided by Java like JDBC and OOPS-based programming.
That’s why now we mostly use servlets instead of CGI
1) init() : Called once per servlet to initialise it.
2) destroy() : Again called only once per servlet at the end of its life-cycle.
3) service() : Method which handles requests. Each request comes in a separate thread.
Servlet is an interface mandating the implementation of above 3 methods.
GenericServlet is an abstract implementation of servlet. It only has service() method as abstract.
HttpServlet extends GenericServlet and its service() method delegates requests into the following 4 methods:
Web Server is a tool that runs servlets. It responds to requests from clients for accessing Internet content. Example, Tomcat, JBoss, Glassfish
Servlet container or Web Server does the following:
|Email:||(Your email is not shared with anybody)|