Friday, April 26, 2024
HomeJavaDistinction between Constructor vs Init methodology in Servlet? Interview Query Reply

Distinction between Constructor vs Init methodology in Servlet? Interview Query Reply


Can we now have a Constructor in Java Servlet? or Why do we’d like a constructor in Servlet if there’s already an init() methodology for initializing Servlet, or what’s the distinction between the init() methodology and constructor in Servlet are a few questions I’ve seen in varied Java internet developer interviews. All of those questions are associated to the position of the constructor and init() methodology within the Servlet implementation class. Although I had shared few ideas on this, after I wrote the prime 10 Servlet questions for Java programmers,  I assumed to cowl it in additional element right here.

On this article, I’ll attempt to reply every of those questions intimately. The important thing factor to recollect is that Servlet is particular within the sense that its life cycle is managed by internet containers like Tomcat and Jetty. They’re accountable for creating cases of Servlets and destroying them after they do not have sufficient sources or have to assist so many cases of Servlets.

Let me first reply the query, Can we create a constructor in Servlets? after which I’ll reply why you have to be utilizing the init() methodology for Servlet initialization by describing the distinction between constructor and init() methodology.

Btw, in case you are new to Servlet and JSP then I additionally recommend you undergo a complete course to study Servlet and JSP in depth. If you happen to want a suggestion then I recommend you take a look at these free Servlet and JSP programs from Coursera and Udemy. 

Can we outline Constructor in Servlet?

The quick reply to this query, Sure, Servlet implementation lessons can have a constructor however they need to be utilizing the init() methodology to initialize Servlet due to two causes, first you can’t declare constructors on an interface in Java.


This implies you can’t implement this requirement to any class which implements the Servlet interface and second, Servlet requires ServletConfig object for initialization which is created by container because it additionally has a reference of ServletContext object, which can be created by the container.

Servlet is an interface outlined in javax.servlet package deal and HttpServlet is a category and like some other class in Java they will have constructors, however you can’t declare constructor inside interface in Java. If you happen to do not present an specific constructor then the compiler will add a default no-argument constructor in any Servlet implementation class.

One more reason that you shouldn’t initialize Servlet utilizing constructor as a result of Servlets are usually not instantly instantiated by Java code, as a substitute containers create their occasion and hold them within the pool.

Since containers from internet servers like Tomcat and Jetty makes use of Java Reflection for creating an occasion of Servlet, the presence of a no-argument constructor is a should. So, by any likelihood, in the event you present a parametric constructor and overlook to write down a no-argument constructor, the net container won’t be able to create an occasion of your Servlet, since there isn’t a default constructor.

Bear in mind Java compiler would not add a default no-argument constructor if there’s a parametric constructor current in school. That is why it isn’t suggested to supply a constructor within the Servlet class. Now let’s examine some variations between Constructor and init methodology in Java Servlet

Distinction between Constructor and init methodology in Servlet

In a real-world utility, you higher use the init() methodology for initialization, as a result of the init() methodology receives a ServletConfig parameter, which can comprise any initialization parameters for that Servlet from the net.xml file.

Since internet.xml gives helpful info to internet containers just like the identify of Servlet to instantiate, the ServletConfig occasion is used to provide initialization parameters to Servlets.

You’ll be able to configure your Servlet primarily based upon settings offered within the ServletConfig object e.g. it’s also possible to present environment-specific settings like the trail of temp listing, database connection parameters (by the way in which for that it is best to higher leverage the JNDI connection pool), and some other configuration parameters.

You’ll be able to merely deploy your internet utility with completely different settings within the internet.xml file on every surroundings. Bear in mind, the init() methodology shouldn’t be chained like a constructor, the place a superclass constructor is named earlier than the subclass constructor executes, also called constructor chaining. See these free Servlet programs to study extra. 

Difference between Constructor vs Init method in Servlet? Interview Question Answer

That is all on this publish in regards to the distinction between constructor and init methodology of Servlet. We have now seen that the container makes use of internet.xml to get Servlet’s identify for initialization and makes use of Java Reflection API, primarily class.newInstance() methodology to create an occasion of Servlet, which suggests Servlet class should want a default no-argument constructor.

We have now additionally seen that why Servlet can’t have a user-defined constructor, primarily as a result of Servlet because the interface can’t assure it and the net container creates an occasion of Servlet and has entry to Context and Config object which isn’t accessible to the developer.

If you happen to like this Java Internet developer Interview query and hungry for extra remember to verify the next questions as properly :

  • What’s the distinction between the Struts 1 and Struts 2 MVC framework? (reply)
  • What’s the distinction between ahead() and sendredirect() methodology? (reply)
  • How you can keep away from double submission of kind knowledge in Servlet? (reply)
  • What’s the load-on-startup tag do in internet.xml? (reply)
  • What’s JSESSIONID in a Java Internet utility? (reply)
  • What number of bean scope is on the market in Spring framework? (reply)
  • Distinction between Setter and Constructor injection in Spring? (reply)
  • What’s the distinction between Internet and Software Server? (reply)
  • Distinction between GET and POST HTTP request? (reply)
  • What’s the distinction between URL encoding and URL Rewriting? (reply)
  • Distinction between embrace directive and embrace motion in JSP? (reply)
  • How you can outline utility large error web page in JSP? (reply)
  • What’s distinction between ServletConfig and ServletContext? (reply)
  • How you can escape XML metacharacters in JSP? (reply)

P. S. – At all times want to init() methodology for initializing Servlet than the constructor, as a result of ServletConfig object is provided to init() methodology with configuration parameters.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments