Constructor Vs Init Method Inward Servlet - Jee Interview Question

Can nosotros bring Constructor inwards Java Servlet? or Why practise nosotros postulate constructor inwards Servlet if at that topographic point is already an init() method for initializing Servlet, or what is divergence betwixt init() method in addition to constructor inwards Servlet are yoke of questions I bring seen inwards diverse Java spider web developer interviews. All of these questions are related to portion of constructor in addition to init() method inwards Servlet implementation class. Though I had shared few thoughts on this, when I wrote top 10 Servlet questions for Java programmers,  I idea to encompass it inwards to a greater extent than exceptional here. In this article, I volition endeavour to reply each of these interrogation inwards detail. Key affair to scream upwardly is that Servlet are special inwards the feel that their life cycle is managed yesteryear spider web container similar Tomcat in addition to Jetty. They are responsible for creating instances of Servlet in addition to destroying them when they don't bring plenty resources or postulate to back upwardly hence many instances of Servlets. Let me root reply the question, Can nosotros practise constructor inwards Servlets? in addition to hence I volition reply why you lot should survive using init() method for Servlet initialization yesteryear describing divergence betwixt constructor in addition to init() method.


Can nosotros define Constructor inwards Servlet?

Short reply of this question, Yes, Servlet implementation classes tin sack bring constructor but they should survive using init() method to initialize Servlet because of 2 reasons, root you lot cannot declare constructors on interface inwards Java, which agency you lot cannot enforce this requirement to whatsoever shape which implements Servlet interface in addition to second, Servlet require ServletConfig object for initialization which is created yesteryear container every bit it likewise has reference of ServletContext object, which is likewise created yesteryear container.


Servlet is an interface defined inwards javax.servlet parcel in addition to HttpServlet is a shape in addition to similar whatsoever other shape inwards Java they tin sack bring constructor, but you lot cannot declare constructor within interface inwards Java. If you lot don't provide an explicit constructor than compiler volition add together a default no declaration constructor inwards whatsoever Servlet implementation class. Another argue that you lot should non initialize Servlet using constructor because Servlets are non conduct instantiated yesteryear Java code, instead container practise at that topographic point instance in addition to buy the farm on them inwards pool. Since containers from spider web servers similar Tomcat in addition to Jetty uses Java Reflection for creating instance of Servlet, presence of no declaration constructor is must. So, yesteryear whatsoever endangerment if you lot provide a parametric constructor in addition to forget to write a no declaration constructor, spider web container volition non survive able to practise instance of your Servlet, since at that topographic point is no default constructor. Remember Java compiler doesn't add together default no declaration constructor, if at that topographic point is a parametric constructor introduce inwards class. That's why it's non advised to provide constructor inwards Servlet class. Now let's come across exactly about divergence betwixt Constructor in addition to init method inwards Java Servlet



Difference betwixt Constructor in addition to init method inwards Servlet

In existent footing application, you lot ameliorate purpose init() method for initialization, because init() method receives a ServletConfig parameter, which may comprise whatsoever initialization parameters for that Servlet from web.xml file. Since web.xml provides useful information to spider web container e.g. refer of Servlet to instantiate, ServletConfig instance is used to provide initialization parameter to Servlets. You tin sack configure your Servlet based upon settings provided inwards ServletConfig object e.g. you lot tin sack likewise provide environs specific settings e.g. path of temp directory, database connective parameters (by the way for that you lot should ameliorate leverage JNDI connective pool) in addition to whatsoever other configuration parameters. You tin sack exactly deploy your spider web application alongside dissimilar settings inwards web.xml file on each environment. Remember, init() method is non chained similar constructor, where super shape constructor is called earlier sub shape constructor executes, likewise known every bit constructor chaining.

 Can nosotros bring Constructor inwards Java Servlet Constructor vs Init method inwards Servlet - JEE Interview Question


That's all on this post service almost difference betwixt constructor in addition to init method of Servlet. We bring seen that container uses web.xml to teach Servlet's refer for initialization in addition to uses Java Reflection API, primarily class.newInstance() method to practise instance of Servlet, which agency Servlet shape must postulate a default no declaration constructor. We bring likewise seen that why Servlet cannot bring user defined constructor, mainly because Servlet every bit interface cannot guarantee it in addition to spider web container creates instance of Servlet in addition to has access to Context in addition to Config object which is non accessible to developer. Always prefer init() method for initializing Servlet than constructor, because ServletConfig object is supplied to init() method alongside configuration parameters.

Further Learning
answer)
  • What is divergence betwixt forward() in addition to sendredirect() method? (answer)
  • How to avoid double submission of shape information inwards Servlet? (answer)
  • What is load-on-startup tag practise inwards web.xml? (answer)
  • What is JSESSIONID inwards Java Web application? (answer)
  • How many edible bean reach is available inwards Spring framework? (answer)
  • Difference betwixt Setter in addition to Constructor injection inwards Spring? (answer)
  • What is divergence betwixt Web in addition to Application Server? (answer)
  • Difference betwixt GET in addition to POST HTTP request? (answer)
  • What is divergence betwixt URL encoding in addition to URL Rewriting? (answer)
  • Difference betwixt include directive in addition to include activity inwards JSP? (answer)
  • How to define application broad fault page inwards JSP? (answer)
  • What is divergence betwixt ServletConfig in addition to ServletContext? (answer)
  • How to escape XML metacharacters inwards JSP? (answer)
  • Komentar

    Postingan populer dari blog ini

    Fixing Java.Net.Bindexception: Cannot Assign Requested Address: Jvm_Bind Inwards Tomcat, Jetty

    5 Deviation Betwixt Constructor In Addition To Static Mill Method Inward Java- Pros In Addition To Cons

    Top V Websites For Practicing Information Structures Together With Algorithms For Coding Interviews Free