This tutorials explains Servlet basics and step by step writing of first Servlet program. The prerequisite for servlet understanding in core java , html & networking concepts. Basically, Servlet is server side programing in java language. Its main goal is to generate dynamic web page, i.e it prepares a response which is in text/html format for the requests from clients.
servlet is programmed with the Java language, so all the Java features such as high portability, platform independence, security and Java database connectivity is applicable. As servlet modules run on the server machine, they receive requests from clients and sends response in html format.
Above figure shows, the life cycle of servlet. As the first request from client browser, the server container that is web server (ex. Tomcat, Glassfish ) locates servlet and create the instance and called init method. It is called only once and not for each user request. so, it is used for one-time initializations.
To process that request , service method get invoked. It is the main method to perform the actual task. The servlet container that is the web server (ex. Tomcat, glassfish) calls the service() method to handle requests coming from the client browser and to write the response in html format & send back to the client. for each request for a servlet, the server assign a new thread and calls service method.according to HTTP request type service method calls doGet , doPost , doPut, doTrace doDelete method.
After the destroy() method is called, the servlet object is marked for garbage collection. The destroy() method is called only once. Here we can put the code for clean activities like close database connections, write cookie lists or hit counts to disk etc.
Install Java Development Kit (JDK) first, then Install Netbeans version above 8 , Please choose setup according to your operating system. during installation select glass fish server or apache tomcat server as shown below.
Following video shows step by step instruction to develop first servlet program using netbeans IDE.