<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":1053,"date":"2021-07-19T12:55:03","date_gmt":"2021-07-19T12:55:03","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=1053"},"modified":"2021-09-09T06:05:47","modified_gmt":"2021-09-09T06:05:47","slug":"can-we-define-constructor-in-servlet-class","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/","title":{"rendered":"Can we define constructor in Servlet class ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"can-we-define-constructor-in-servlet-class\" class=\"color-pink\" style=\"text-align: justify;\">Can we define constructor in Servlet class ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Yes, we can define constructor in Servlet class.<\/li>\n<li>Constructor may be a Default Constructor or Parameterised Constructor. If we didn&#8217;t write any constructor then the default constructor will be executed. Because, to read init parameters of a servlet we need servlet config object in the life cycle of the servlet.<\/li>\n<li>Servlet config object is created after the execution of the constructor.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"text-center row\" style=\"text-align: justify;\"><\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"servlet-constructor\" class=\"color-green\">Servlet Constructor<\/h2>\n<\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"sample-code\" class=\"color-purple\">Sample Code<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">import javax.servlet.http.*;<br\/>import java.io.*;<br\/><br\/>public class ConstructorServlet extends HttpServlet<br\/>{<br\/>  String str1, str2, str3;<br\/>  public ConstructorServlet()<br\/>  {<br\/>    str1 = &quot;Constructor&quot;;<br\/>  }<br\/>  public void init()<br\/>  {<br\/>    str2 = &quot;init() method&quot;;<br\/>  }<br\/>  public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException<br\/>  {<br\/>    response.setContentType(&quot;text\/html&quot;);<br\/>    PrintWriter out = response.getWriter();<br\/><br\/>    str3 = &quot; service() method&quot;;<br\/><br\/>    out.println(&quot;&lt;h3&gt;&quot;+ str1 + &quot;&lt;br&gt;&quot; + str2 + &quot;&lt;br&gt;&quot; + str3 + &quot;&lt;\/h3&gt;&quot;); <br\/>    out.close();<br\/>  }<br\/>} <\/code><\/pre> <\/div>\n<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-blue\">Output<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\"> Constructor<br\/>    init() method<br\/>    service() method<\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : Yes, we can define constructor in Servlet class&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[7421],"tags":[195,7459,1119,360,7422,7423,203,199,214,363,209,7434,7455,205,7424,222,484,7453,7425,7439,196,212,5941,7436,7438,7446,7442,7441,286,207,366,15929,208,288,7458,206,7437,7426,7444,7440,7443,7427,7428,7429,7451,7435,7430,7445,7447,7448,7431,7454,7449,200,197,280,7450,7457,7452,7432,7433,968,216,7456,15894],"class_list":["post-1053","post","type-post","status-publish","format-standard","hentry","category-java-servlet","tag-accenture-interview-questions-and-answers","tag-allstate-solution-interview-questions-and-answers","tag-att-interview-questions-and-answers","tag-atos-interview-questions-and-answers","tag-can-servlet-class-declare-constructor-with-servletconfig-object-as-a-argument","tag-can-we-write-parameterized-constructor-in-servlet","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-constructor-in-servlet-java-example","tag-define-constructor-in-servlet-class","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-difference-between-constructor-and-init-method-of-servlet","tag-flipkart-interview-questions-and-answers","tag-genpact-interview-questions-and-answers","tag-httpservlet","tag-httpservlet-class-source-code","tag-httpservletrequest","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-infogain-interview-questions-and-answers","tag-init-method-in-servlet","tag-java-servlet","tag-java-servlet-example","tag-jsp-and-servlet","tag-jsp-servlet","tag-lt-infotech-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-polaris-financial-technology-interview-questions-and-answers","tag-prokarma-softech-pvt-ltd-interview-questions-and-answers","tag-r-systems-interview-questions-and-answers","tag-rbs-india-developmen-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-servlet","tag-servlet-apiservlet-example","tag-servlet-container","tag-servlet-example","tag-servlet-filter","tag-servlet-init-method","tag-servlet-initialization","tag-servlet-interface","tag-servlet-interview-questions","tag-servlet-is-java-class-then-why-there-is-no-constructor-in-servlet","tag-servlet-life-cycle","tag-servlet-mapping","tag-servlet-program","tag-servlet-session","tag-servlet-structure","tag-servletcontext","tag-servletrequest","tag-tech-mahindra-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-webservlet","tag-wells-farg-interview-questions-and-answers","tag-what-is-servlet","tag-why-do-we-need-constructor-in-servlet-even-though-we-have-a-init-method","tag-why-no-constructor-in-servletgenericservlet-class","tag-wipro-infotech-interview-questions-and-answers","tag-wipro-interview-questions-and-answers","tag-xoriant-solution-interview-questions-and-answers","tag-yugma-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Can we define constructor in Servlet class ? - Java Servlet<\/title>\n<meta name=\"description\" content=\"Can we define constructor in Servlet class ? - Java Servlet - Yes, we can define constructor in Servlet class.Constructor may be a Default Constructor.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Can we define constructor in Servlet class ? - Java Servlet\" \/>\n<meta property=\"og:description\" content=\"Can we define constructor in Servlet class ? - Java Servlet - Yes, we can define constructor in Servlet class.Constructor may be a Default Constructor.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-19T12:55:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-09T06:05:47+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/\",\"name\":\"Can we define constructor in Servlet class ? - Java Servlet\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2021-07-19T12:55:03+00:00\",\"dateModified\":\"2021-09-09T06:05:47+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"Can we define constructor in Servlet class ? - Java Servlet - Yes, we can define constructor in Servlet class.Constructor may be a Default Constructor.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Can we define constructor in Servlet class ? - Java Servlet","description":"Can we define constructor in Servlet class ? - Java Servlet - Yes, we can define constructor in Servlet class.Constructor may be a Default Constructor.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/","og_locale":"en_US","og_type":"article","og_title":"Can we define constructor in Servlet class ? - Java Servlet","og_description":"Can we define constructor in Servlet class ? - Java Servlet - Yes, we can define constructor in Servlet class.Constructor may be a Default Constructor.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/","og_site_name":"Wikitechy","article_published_time":"2021-07-19T12:55:03+00:00","article_modified_time":"2021-09-09T06:05:47+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/","name":"Can we define constructor in Servlet class ? - Java Servlet","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-19T12:55:03+00:00","dateModified":"2021-09-09T06:05:47+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"Can we define constructor in Servlet class ? - Java Servlet - Yes, we can define constructor in Servlet class.Constructor may be a Default Constructor.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/java-servlet\/can-we-define-constructor-in-servlet-class\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=1053"}],"version-history":[{"count":3,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1053\/revisions"}],"predecessor-version":[{"id":3103,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/1053\/revisions\/3103"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}