{"id":16092,"date":"2016-09-12T11:00:52","date_gmt":"2016-09-12T11:00:52","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=16092"},"modified":"2020-09-01T07:02:36","modified_gmt":"2020-09-01T07:02:36","slug":"introduction-to-servlets","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/introduction-to-servlets\/","title":{"rendered":"Introduction to Servlets"},"content":{"rendered":"<p><span lang=\"EN-US\">Keeping in tune with the OCEJWCD 6 exam and its objectives, we will start a series of posts that will explain the concepts of the exam. This first post will discuss the basic concepts related to servlets and its life cycle.<\/span><\/p>\n<h2><span lang=\"EN-US\">What are the basic terminologies for the exam?<\/span><\/h2>\n<p><b><span lang=\"EN-US\">Client and Server:<\/span><\/b><\/p>\n<p><span lang=\"EN-US\">A \u201cclient\u201d is any user who makes certain requests to the server. The requests may be simple examples as fetching a document or modifying a shopping cart.<\/span><\/p>\n<p><span lang=\"EN-US\">A \u201cserver\u201d is a device or a program that returns the resources requested by the \u201cclient\u201d. Most of the Internet runs on the client-server architecture.<\/span><\/p>\n<p><b><span lang=\"EN-US\">Static pages and Dynamic pages<\/span><\/b><span lang=\"EN-US\">:<\/span><\/p>\n<p><span lang=\"EN-US\">\u201cStatic pages\u201d are those that are returned \u201cas is\u201d to the client. An example may be fetching a document \u2018xyz.html\u2019 from the server. \u00a0<\/span><\/p>\n<p><span lang=\"EN-US\">\u201cDynamic pages\u201d on the other hand are more interactive and user friendly. Pages that change according to user preferences, is one example of a dynamic page. The content on dynamic web sites, are not stale and are constantly updated. <\/span><\/p>\n<p><b><span lang=\"EN-US\">\u00a0Web components and Web applications:<\/span><\/b><\/p>\n<p><span lang=\"EN-US\">Servlets and JSPs are considered to be web components.\u00a0 A web application is a combination of web components and static pages.<\/span><\/p>\n<p><b><span lang=\"EN-US\">Servlets, CGI and Web container:<\/span><\/b><\/p>\n<p><span lang=\"EN-US\">A \u201cservlet\u201d is a small program that runs inside a server. A servlet is responsible for doing most of the processing that has to be done by the application. A servlet is hence used to create dynamic and interactive pages. In the days prior to servlets, CGI was used to create dynamic pages.\u00a0 CGI scripts were slow and less secure than servlets. This was one of the reasons that they gave way to servlets in due course of time.<\/span><\/p>\n<p><span lang=\"EN-US\">Web applications (servlets, JSPs and static pages) are managed by a web container. The primary function of a web container is to manage interactions between the different components in a web application and to manage the servlet life cycle. <\/span><\/p>\n<p><span lang=\"EN-US\">Having seen the important definitions related to the Web component developer exam, let us move onto a discussion of servlets.<\/span><\/p>\n<h2><span lang=\"EN-US\">What are the important Servlets classes and interfaces?<\/span><\/h2>\n<p><span lang=\"EN-US\">In order to create servlets and work with them it is necessary to extend the \u2018GenericServlet\u2019 abstract class. If we have to work with Http Servlets specifically, it is necessary to extend the \u2018HttpServlet\u2019 abstract class. It should be noted that \u2018Servlet\u2019 itself is an interface. <\/span><\/p>\n<p><span lang=\"EN-US\">It should also be borne in mind that the resource requested by a client is the \u201c<b>request<\/b>\u201d and the reply from the server is the \u201c<b>response<\/b>\u201d. <\/span><\/p>\n<p><span lang=\"EN-US\">This brings us to two other important objects for the exam \u2013 the \u2018HttpServletRequest\u2019 interface and \u2018HttpServletResponse\u2019 interface. \u00a0The \u2018HttpServletRequest\u2019 and \u2018HttpServletResponse\u2019 interfaces are related to the HTTP servlets. <\/span><\/p>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2016\/08\/req-res.png\"><img decoding=\"async\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2016\/08\/req-res.png\" alt=\"req-res\" width=\"883\" height=\"517\" class=\"aligncenter size-full wp-image-16093\" \/><\/a><\/p>\n<p><span lang=\"EN-US\">This is the hierarchy of the \u2018HttpServletRequest\u2019 and \u2018HttpServletResponse\u2019 interface:<\/span><\/p>\n<p><a href=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2016\/08\/servlet-class.jpg\"><img decoding=\"async\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2016\/08\/servlet-class.jpg\" alt=\"servlet-class\" width=\"630\" height=\"299\" class=\"aligncenter size-full wp-image-16094\" \/><\/a><\/p>\n<p><span lang=\"EN-US\">All servlets must override one of the following methods (which is defined in the HttpServlet class)<\/span><\/p>\n<p><span lang=\"EN-US\">doGet()<\/span><\/p>\n<p><span lang=\"EN-US\">doPost()<\/span><\/p>\n<p><span lang=\"EN-US\">doPut()<\/span><\/p>\n<p><span lang=\"EN-US\">doDelete()<\/span><\/p>\n<p><span lang=\"EN-US\">\u00a0<\/span><\/p>\n<p><span lang=\"EN-US\">There are three other doXXX() methods as well \u2013 they are doOptions(), doTrace() and doHead()<\/span><\/p>\n<h2><span lang=\"EN-US\">Servlet life cycle:<\/span><\/h2>\n<p><span lang=\"EN-US\">All servlets are characterized by the init(), service() and destroy() methods. Let us briefly see the servlet life cycle:<\/span><\/p>\n<ol>\n<li><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">The init() method is where the initialization of the servlet takes place. It takes place only once in a servlet life cycle. The init() method may be overridden.<\/span><\/li>\n<li><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">The servlet spends most of its time in the service() method. The service() method must not be overridden. The service() method in turn calls one of the doXXX() methods mentioned above.<\/span><\/li>\n<li><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">The destroy() method is called once the response is generated and sent back to the client. The destroy() method is again called only once and the final clean up of the servlet occurs here.<\/span><\/li>\n<\/ol>\n<p>We have seen a brief introduction of the web component developer exam terminologies, the servlet interface and a crisp understanding of the servlet life cycle. We will understand more of servlets in the next post.<\/p>\n<p><span>Preparing for OCEJWCD 6\u00a0Certification? Pass in 1st attempt through <\/span><span style=\"color: #0000ff\"><a href=\"https:\/\/www.whizlabs.com\/oracle-certified-expert-java-ee-6-web-component-developer\/\" style=\"color: #0000ff\">Whizlabs OCEJWCD 6 Course! <\/a><\/span><span>Start with Free Trial!<\/span><\/p>\n<p><span lang=\"EN-US\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Keeping in tune with the OCEJWCD 6 exam and its objectives, we will start a series of posts that will explain the concepts of the exam. This first post will discuss the basic concepts related to servlets and its life cycle. What are the basic terminologies for the exam? Client and Server: A \u201cclient\u201d is any user who makes certain requests to the server. The requests may be simple examples as fetching a document or modifying a shopping cart. A \u201cserver\u201d is a device or a program that returns the resources requested by the \u201cclient\u201d. Most of the Internet runs [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[951,1145],"class_list":["post-16092","post","type-post","status-publish","format-standard","hentry","category-java","tag-introduction-to-servlets","tag-ocejwcd-6"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":4,"uagb_excerpt":"Keeping in tune with the OCEJWCD 6 exam and its objectives, we will start a series of posts that will explain the concepts of the exam. This first post will discuss the basic concepts related to servlets and its life cycle. What are the basic terminologies for the exam? Client and Server: A \u201cclient\u201d is&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=16092"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16092\/revisions"}],"predecessor-version":[{"id":71639,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16092\/revisions\/71639"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=16092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=16092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=16092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}