Constructor Vs Init Method Inwards Servlet - Jee Interview Question

Can nosotros convey Constructor inwards Java Servlet? or Why practise nosotros postulate constructor inwards Servlet if in that place is already an init() method for initializing Servlet, or what is deviation betwixt init() method too constructor inwards Servlet are couplet of questions I convey seen inwards diverse Java spider web developer interviews. All of these questions are related to business office of constructor too 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 endeavor to reply each of these query inwards detail. Key matter to retrieve is that Servlet are special inwards the feel that their life bike is managed past times spider web container similar Tomcat too Jetty. They are responsible for creating instances of Servlet too destroying them when they don't convey plenty resources or postulate to back upward thence many instances of Servlets. Let me get-go reply the question, Can nosotros practise constructor inwards Servlets? too and then I volition reply why you lot should survive using init() method for Servlet initialization past times describing deviation betwixt constructor too init() method.


Can nosotros define Constructor inwards Servlet?

Short reply of this question, Yes, Servlet implementation classes tin sack convey constructor but they should survive using init() method to initialize Servlet because of ii reasons, get-go you lot cannot declare constructors on interface inwards Java, which agency you lot cannot enforce this requirement to whatever class which implements Servlet interface too second, Servlet require ServletConfig object for initialization which is created past times container every bit it too has reference of ServletContext object, which is too created past times container.


Servlet is an interface defined inwards javax.servlet packet too HttpServlet is a class too similar whatever other class inwards Java they tin sack convey 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 whatever Servlet implementation class. Another argue that you lot should non initialize Servlet using constructor because Servlets are non straight instantiated past times Java code, instead container practise in that place illustration too continue them inwards pool. Since containers from spider web servers similar Tomcat too Jetty uses Java Reflection for creating illustration of Servlet, presence of no declaration constructor is must. So, past times whatever peril if you lot provide a parametric constructor too forget to write a no declaration constructor, spider web container volition non survive able to practise illustration of your Servlet, since in that place is no default constructor. Remember Java compiler doesn't add together default no declaration constructor, if in that place is a parametric constructor introduce inwards class. That's why it's non advised to provide constructor inwards Servlet class. Now let's run across to a greater extent than or less deviation betwixt Constructor too init method inwards Java Servlet



Difference betwixt Constructor too init method inwards Servlet

In existent basis application, you lot meliorate role init() method for initialization, because init() method receives a ServletConfig parameter, which may incorporate whatever initialization parameters for that Servlet from web.xml file. Since web.xml provides useful information to spider web container e.g. cite of Servlet to instantiate, ServletConfig instance is used to render initialization parameter to Servlets. You tin sack configure your Servlet based upon settings provided inwards ServletConfig object e.g. you lot tin sack too provide surroundings specific settings e.g. path of temp directory, database connector parameters (by the way for that you lot should meliorate leverage JNDI connector pool) too whatever other configuration parameters. You tin sack but deploy your spider web application amongst dissimilar settings inwards web.xml file on each environment. Remember, init() method is non chained similar constructor, where super class constructor is called earlier sub class constructor executes, too known every bit constructor chaining.

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


That's all on this postal service close difference betwixt constructor too init method of Servlet. We convey seen that container uses web.xml to instruct Servlet's cite for initialization too uses Java Reflection API, primarily class.newInstance() method to practise illustration of Servlet, which agency Servlet class must postulate a default no declaration constructor. We convey too seen that why Servlet cannot convey user defined constructor, mainly because Servlet every bit interface cannot guarantee it too spider web container creates illustration of Servlet too has access to Context too 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 amongst configuration parameters.

Further Learning
answer)
  • What is deviation betwixt forward() too 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 ambit is available inwards Spring framework? (answer)
  • Difference betwixt Setter too Constructor injection inwards Spring? (answer)
  • What is deviation betwixt Web too Application Server? (answer)
  • Difference betwixt GET too POST HTTP request? (answer)
  • What is deviation betwixt URL encoding too URL Rewriting? (answer)
  • Difference betwixt include directive too include activity inwards JSP? (answer)
  • How to define application broad fault page inwards JSP? (answer)
  • What is deviation betwixt ServletConfig too ServletContext? (answer)
  • How to escape XML metacharacters inwards JSP? (answer)
  • 0 Response to "Constructor Vs Init Method Inwards Servlet - Jee Interview Question"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel