<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rajitha Yasasri</title>
    <description>The latest articles on DEV Community by Rajitha Yasasri (@ryasasri).</description>
    <link>https://dev.to/ryasasri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F951317%2F1a754ccd-3c26-4d7f-9ff7-3e414791e25a.jpg</url>
      <title>DEV Community: Rajitha Yasasri</title>
      <link>https://dev.to/ryasasri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ryasasri"/>
    <language>en</language>
    <item>
      <title>Servlet Life Cycle</title>
      <dc:creator>Rajitha Yasasri</dc:creator>
      <pubDate>Fri, 13 Jan 2023 20:18:36 +0000</pubDate>
      <link>https://dev.to/ryasasri/servlet-life-cycle-2khn</link>
      <guid>https://dev.to/ryasasri/servlet-life-cycle-2khn</guid>
      <description>&lt;h2&gt;
  
  
  What are the Servlets?
&lt;/h2&gt;

&lt;p&gt;Servlets are Java Programming language classes that dynamically process requests and construct responses. Servlets are on the server side. Those are pure java classes and unlike JSPs, only Java codes can be written in the servlets with the file extension of .java.  In Java web applications, servlets are suitable to work with the database, and session management, use the persistence API, invoke a web service, and do RMI call. In Enterprise Java Beans (EJB) applications, Servlets are used to invoke Java beans. Servlets contain the back-end logic of a java web application in most cases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnttk6duvkqo4j6svil8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnttk6duvkqo4j6svil8o.png" alt="A sample Servlet" width="800" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Class Hierarchy
&lt;/h2&gt;

&lt;p&gt;Following are the parent classes of &lt;em&gt;HttpServlet&lt;/em&gt; class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo0rkyl4i2nw8un51j5qf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo0rkyl4i2nw8un51j5qf.png" alt="Class hierarchy of _HttpServlet_ class depicting only the important servlet methods." width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Life Cycle
&lt;/h2&gt;

&lt;p&gt;From the creation of a Servlet to the termination of it is called the “Servlet life cycle”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6hou3yfq8qzcdda8mc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy6hou3yfq8qzcdda8mc5.png" alt="Servlet life cycle" width="800" height="1164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s consider the very first request received by a Servlet. Since this is the first request to the Servlet there are not any instances of that Servlet. Then the Servlet gets compiled to a class file and the web container creates a new instance and the &lt;em&gt;init(ServletConfig config)&lt;/em&gt; method is invoked. Then the &lt;em&gt;init()&lt;/em&gt; method is invoked. These &lt;em&gt;init()&lt;/em&gt; methods can be used to initialize resources such as JDBC connections. The &lt;em&gt;init()&lt;/em&gt; methods are called once in the life cycle by the web container.&lt;/p&gt;

&lt;p&gt;After the invocation of the &lt;em&gt;init&lt;/em&gt; methods, three objects are created. First, a thread object is created to handle this process afterward (to serve the received request); A &lt;em&gt;HTTPServletRequest&lt;/em&gt; object is created as the request; Lastly, A &lt;em&gt;HTTPServletResponse&lt;/em&gt; object is created as the response. This is the starting point for the requests which are not the very first request (if there is already a servlet instance available). This means that for every request, a thread is created, an &lt;em&gt;HTTPServletRequest&lt;/em&gt; is created and &lt;em&gt;HTTPServletResponse&lt;/em&gt; is created. &lt;/p&gt;

&lt;p&gt;Then the &lt;em&gt;Service(ServletRequest request, ServletResponse response)&lt;/em&gt; method and then the &lt;em&gt;Service(HTTPServletRequest request, HTTPServletResponse response)&lt;/em&gt; is invoked. Previously created &lt;em&gt;HTTPServletRequest&lt;/em&gt; object and &lt;em&gt;HTTPServletResponse&lt;/em&gt; object are passed to these methods as the parameter list. These Service methods are invoked by the web container whenever a request from a client is received. The latter method checks the received HTTP header for the HTTP method type. Then it invokes the &lt;em&gt;doGet()&lt;/em&gt;, &lt;em&gt;doPost()&lt;/em&gt;, etc., methods accordingly. The method can be overridden and write program code if required.&lt;/p&gt;

&lt;p&gt;The service method invokes 7 methods considering the type of the HTTP request received. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;em&gt;doGet()&lt;/em&gt; for GET&lt;/li&gt;
&lt;li&gt; &lt;em&gt;doPost()&lt;/em&gt; for POST&lt;/li&gt;
&lt;li&gt; &lt;em&gt;doDelete()&lt;/em&gt; for DELETE&lt;/li&gt;
&lt;li&gt; &lt;em&gt;doUpdate()&lt;/em&gt; for UPDATE&lt;/li&gt;
&lt;li&gt; &lt;em&gt;doOptions()&lt;/em&gt; for OPTIONS&lt;/li&gt;
&lt;li&gt; &lt;em&gt;doHead()&lt;/em&gt; for HEAD&lt;/li&gt;
&lt;li&gt; &lt;em&gt;doTrace()&lt;/em&gt; for TRACE &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most all the time the program logic goes here by overriding these methods according to the need. The argument lists of these methods are types of &lt;em&gt;HTTPServletRequest&lt;/em&gt; and &lt;em&gt;HTTPServletResponse&lt;/em&gt;. But those are interfaces therefore they can’t be instantiated. The methods are receiving objects of types of &lt;em&gt;RequestFacade&lt;/em&gt; and &lt;em&gt;ResponseFacade&lt;/em&gt; as their parameter list. These are classes and they can be instantiated. &lt;/p&gt;

&lt;p&gt;Finally, the &lt;em&gt;destroy()&lt;/em&gt; method is invoked by the web container to terminate the servlet. It is called once in the servlet lifecycle. This method can be overridden and suitable to close database connections and files, etc.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>softwaredevelopment</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
