HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Servlet life cycle can be defined as the whole process from creation through destruction. The following is the procedure to follow Servlet:
Let us now discuss in detail the life cycle approach.
init method is designed to be called only once. It is invoked when you first create a Servlet, at each subsequent user requests no longer call. Therefore, it is used for one-time initialization, like Applet init method.
When users create a Servlet corresponding to the first call to the Servlet URL, but you can also specify the Servlet is loaded when the server is first started.
When the user invokes a Servlet, it will create a Servlet instance, each user request will generate a new thread, the appropriate time transfer to the doGet or doPost method. init () method simply create or load some data that will be used throughout the life cycle Servlet.
Init method is defined as follows:
public void init () throws ServletException { // Initialization code /en. }
service () method is the main method performs the actual task. Servlet container (ie, Web server) calls the service () method to handle requests from the client (browser) and the response format to write back to the client.
Each time the server receives a Servlet request, the server will generate a new thread and calls the service. service () method checks the HTTP request type (GET, POST, PUT, DELETE, etc.), and calls doGet, doPost, doPut, doDelete methods at the appropriate time.
The following is the method characterized by:
public void service (ServletRequest request, ServletResponse response) throws ServletException, IOException { }
service () method is called by the container, service method calls doGet, doPost, doPut, doDelete methods at the appropriate time. So, you do not have to service () method to do anything, you only need to type a request from a client to override doGet () or doPost () can be.
doGet () and doPost () method of each service request is the most commonly used method. Here are the characteristics of these two methods.
GET requests from normal requests a URL, or from a METHOD unspecified HTML form, it is processed by the doGet () method.
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } // Servlet Code
POST request comes from a specially designated METHOD is POST HTML form, it is processed by the doPost () method.
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } // Servlet Code
destroy () method is called only once, to be called at the end of Servlet life cycle. destroy () method allows you to turn off the Servlet database connection, stop the background thread, list, or click on the Cookie counter written to disk, and perform other similar clean-up activities.
After the call to destroy () method, servlet object is marked for garbage collection. destroy method is defined as follows:
public void destroy () { // Termination of the code /en. }
The following figure shows a typical Servlet lifecycle approach.