Servlet follows a certain path from it's creation to it's destruction. This entire process is the Servlet's life cycle.
Here web container has a big role in maintaining the servlet's instance/ object. So before the main thing we need to know a little about the basics.
Here, Web container( Servlet container) is nothing but the component of a web server(such as Tomcat) that interacts with Java servlets.
Basically when request is passed by a client in your browser it is delegated to the servlet container like Tomcat, Jboss etc. Then it will search the equivalent servlet class in the deployment descripter (web.xml file). This is simply an xml file that resides in app's WAR file under WEB-INF/ directory as shown in the fig. below.
fig1.
Here in web.xml file, mapping between the URL paths and servlets is done. It is also needed for security and authentication requirement.
Secure connection is an issue for both the request and response data. They are to be encrypted by the sender before they are transmitted and decrypted by the recipient. It is essential when it comes to protecting customer data, such as contact information, passwords, and confidential messages.
You can see in the fig. below how servlet mapping is done in web.xml file.
<servlet-class>package name.class name</servlet-class
Servlet Lifecycle:
Lets jump into the lifecycle of Servlet to make it much clearer. The lifecycle consists of following steps:
2 Create Instance of Servlet
3. Call the Servlets init() Method
4. Call the Servlets service() Method
5. Call the Servlets destroy() Method
Step 1, 2 and 3 are executed only once, when the servlet is initially loaded.
Step 4 is executed multiple times- once for every Http request to the servlet
and Step 5 is executed when the servlet container unloads the servlet
Step 4 is executed multiple times- once for every Http request to the servlet
and Step 5 is executed when the servlet container unloads the servlet
Before a servlet can be invoked the servlet container must first load its class definition. This is done just like any other class is loaded.
2. Instantiation:
Then the web container internally creates an object of the servlet after loading it. It's instance is created only once in the server's lifecycle.
3. Initialization:
Following is the example of the GenericServlet implementing the Servlet interface which contains init() method within it.
fig.3
Our servlet class is formed when the class extends the HttpServlet class of Java. HttpServlet class internally extends GenericServlet which implements Servlet interface. This is a brief overview of the architechture of servlet class.
Following is the example of the GenericServlet implementing the Servlet interface which contains init() method within it.
fig.3
So, after instantiation server here first calls the init()method present in GenericServlet which our servlet class extends. The code is internally added by Java so we need not focus on it. This step is called initialization.
4. Service:
The web container then calls the service() method in the HttpServlet which our servlet class extends.The service() method is the main method to perform the actual task. It handles requests coming from the client and sends response back to the client.
Here the service() method checks the Http request type (GET, POST,PUT, DELETE etc.) and calls the doGet(), doPost(), doPut() or doDelete() etc, whichever method required. So, we don't have to concern ourselves with service() method but just have to override either doGet() or doPost() method in our servlet class depending on the type of request.
In the above figure, ActiveInvestors is our servlet class that extends HttpServlet. The server directly calls service()method after initialization as in case of init() method. Therefore, while programming we need only to focus on the code we write in doGet() or doPost() method as above~
doGet() and doPost() methods are the most frequently used methods in each service requests.
Following is the signature style of these two methods~
doGet() and doPost() methods are the most frequently used methods in each service requests.
Following is the signature style of these two methods~
Whenever the user sends the request to the server then it generates two objects, first HttpServletRequest 'req' and second HttpServletRequest 'resp'. 'req' represents the client's request and 'resp' represents the servlet's response.
In fig 4. 'resp' is an object of HttpServletResponse class, the second and third line calls method SetContentType() and getWriter() method. SetContentType() basically tells the browser what content type it is sending. In the above case it is text/html.
However it is considered poor pratice as HTML code belongs in a JSP file but for learning purposes only I have shown it this way. To send character data into the browser we use the PrintWriter object ('out') returned by getWriter().
5. Destroy:
The destroy() method is called only one at the end of the servlet's lifecycle. It is called before the servlet instance is removed by the service. It gives the servlet to close the database connections, clean up any resources sucha as memories, cookies, threads etc.
No comments:
Post a Comment