{"id":1494,"date":"2011-02-12T23:59:39","date_gmt":"2011-02-12T18:29:39","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1494"},"modified":"2011-02-12T23:59:39","modified_gmt":"2011-02-12T18:29:39","slug":"life-cycle-of-servlet","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/life-cycle-of-servlet\/","title":{"rendered":"Life Cycle of Servlet"},"content":{"rendered":"<p>Servlet is used in J2EE to create the dynamic web pages under Java\u00a0Environment. \u00a0Like Applets, Servlet also have init() and destroy() methods. In this article i will explain the Life cycle of servlet.<\/p>\n<p><strong>Step 1 <\/strong>: After Compilation of the Servlet, the class file is loaded by the loader.<\/p>\n<p><strong>Step 2 <\/strong>: Then the Container instantiates the Servlet class by calling default Constructor. <a href=\"https:\/\/jitendrazaa.com\/blog\/java\/servlet\/how-container-handles-the-servlet-request\/\" target=\"_blank\">Read this article to know more that how container works for Servlet<\/a>.<\/p>\n<figure id=\"attachment_1496\" aria-describedby=\"caption-attachment-1496\" style=\"width: 452px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Servlet-Life-Cycle.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1496  \" title=\"Servlet Life Cycle\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Servlet-Life-Cycle.jpg?resize=452%2C305&#038;ssl=1\" alt=\"Servlet Life Cycle\" width=\"452\" height=\"305\" \/><\/a><figcaption id=\"caption-attachment-1496\" class=\"wp-caption-text\">Servlet Life Cycle<\/figcaption><\/figure>\n<p><strong><!--more-->Step 3<\/strong>: At Step 2, the Servlet object is just normal java object. <strong>It is the init() method which is just called after the\u00a0instantiation of\u00a0object and gives the Servletness to object. Like applets, this method is called only once in life cycle. <\/strong><a href=\"https:\/\/jitendrazaa.com\/blog\/java\/example-to-override-the-init-method-of-the-servlet\/\" target=\"_blank\">It is\u00a0recommended not to override the init() method but if you want to override then read this article.<\/a><\/p>\n<p><strong>Step 4 : <\/strong>Then <strong>Service()<\/strong> method is called and this is the place <strong>where servlet spends most of its life. <\/strong>Each request here comes as a separate thread. \u00a0This method then internally calls the doGet() or doPost() method depending upon the type of request comes.<\/p>\n<p><strong>Step 5 :<\/strong> <strong>destroy()<\/strong> method is called just before destroying the Servlet. This method should be used if any clean code needs to be run before destroying the servlet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explain Life Cycle of Servlet<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[125],"class_list":["post-1494","post","type-post","status-publish","format-standard","hentry","category-servlet","tag-j2ee"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1464,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/what-is-preinitialization-of-servlet\/","url_meta":{"origin":1494,"position":0},"title":"What is preinitialization of servlet","author":"Jitendra","date":"February 8, 2011","format":false,"excerpt":"What is preinitialization of servlet","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1467,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/difference-between-servletcontext-and-servletconfig\/","url_meta":{"origin":1494,"position":1},"title":"Difference between servletContext and ServletConfig","author":"Jitendra","date":"February 8, 2011","format":false,"excerpt":"What is difference between servletContext and ServletConfig?","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1482,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/how-container-handles-the-servlet-request\/","url_meta":{"origin":1494,"position":2},"title":"How container handles the Servlet request","author":"Jitendra","date":"February 12, 2011","format":false,"excerpt":"How container handles the Servlet request or How the apache Tomcat works","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"Client Browse Servlet URL","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Client-Browse-Servlet-URL.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1976,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/j2ee-servlet-jsp-jsf-and-jms-version-table\/","url_meta":{"origin":1494,"position":3},"title":"J2EE &#8211; Servlet , JSP , JSF and JMS Version Table","author":"Jitendra","date":"April 13, 2011","format":false,"excerpt":"J2EE - Servlet , JSP , JSF and JMS Version Table","rel":"","context":"In &quot;JSP&quot;","block_context":{"text":"JSP","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/jsp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1959,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/create-servlet-using-annotation\/","url_meta":{"origin":1494,"position":4},"title":"Create Servlet using Annotation &#8211; Servlet 3.0","author":"Jitendra","date":"April 13, 2011","format":false,"excerpt":"Create the Servlet without any deployment descriptor. Using Annotations","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1479,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/example-to-override-the-init-method-of-the-servlet\/","url_meta":{"origin":1494,"position":5},"title":"Example to Override the init() method of the servlet","author":"Jitendra","date":"February 11, 2011","format":false,"excerpt":"Example to Override the init() method of the servlet","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1494"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1494\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}