Can we have Constructor in Java Servlet? or Why do we need constructor in Servlet if there is already an init() method for initializing Servlet, or what is difference between init() method and constructor in Servlet are couple of questions I have seen in various Java web developer interviews. All of these questions are related to role of constructor and init() method in Servlet implementation class. Though I had shared few thoughts on this, when I wrote top 10 Servlet questions for Java programmers, I thought to cover it in more detail here. In this article, I will try to answer each of these question in detail. Key thing to remember is that Servlet are special in the sense that their life cycle is managed by web container like Tomcat and Jetty. They are responsible for creating instances of Servlet and destroying them when they don't have enough resource or need to support so many instances of Servlets. Let me first answer the question, Can we create constructor in Servlets? and then I will answer why you should be using init() method for Servlet initialization by describing difference between constructor and init() method.
CAN WE DEFINE CONSTRUCTOR IN SERVLET?
Short answer of this question, Yes, Servlet implementation classes can have constructor but they should be using init() method to initialize Servlet because of two reasons, first you cannot declare constructors on interface in Java, which means you cannot enforce this requirement to any class which implements Servlet interface and second, Servlet requireServletConfig object for initialization which is created by container as it also has reference of ServletContext object, which is also created by container.Servlet is an interface defined in javax.servlet package and HttpServlet is a class and like any other class in Java they can have constructor, but you cannot declare constructor inside interface in Java. If you don't provide an explicit constructor than compiler will add a default no argument constructor in any Servlet implementation class. Another reason that you should not initialize Servlet using constructor because Servlets are not directly instantiated by Java code, instead container create there instance and keep them in pool. Since containers from web
DIFFERENCE BETWEEN CONSTRUCTOR AND INIT METHOD IN SERVLET
In real world application, you better use init() method for initialization, becauseinit() method receives a ServletConfig parameter, which may contain any initialization parameters for that Servlet from web.xml file. Since web.xml provides useful information to web container e.g. name of Servlet to instantiate, ServletConfig instance is used to supply initialization parameter to Servlets. You can configure your Servlet based upon settings provided in ServletConfig object e.g. you can also provide environment specific settings e.g. path of temp directory, database connection parameters (by the way for that you should better leverage JNDI connection pool) and any other configuration parameters. You can simply deploy your web application with different settings in web.xml file on each environment. Remember, init() method is not chained like constructor, where super class constructor is called before sub class constructor executes, also known as constructor chaining.That's all on this post about difference between constructor and init method of Servlet. We have seen that container uses web.xml to get Servlet's name for initialization and uses Java Reflection API, primarily class.newInstance() method to create instance of Servlet, which means Servlet class must need a default no argument constructor. We have also seen that why Servlet cannot have user defined constructor, mainly because Servlet as interface cannot guarantee it and web container creates instance of Servlet and has access to Context and Config object which is not accessible to developer. Always prefer init()method for initializing Servlet than constructor, because ServletConfig object is supplied to init() method with configuration parameters.
If you like this Java Web developer Interview question and hungry for more don't forget to check following questions as well :
- What is difference between
Struts 1 and Struts 2 MVC framework? (answer) - What is difference between forward() and sendredirect() method? (answer)
- How to avoid double submission of form data in Servlet? (answer)
- What is load-on-startup tag do in web.xml? (answer)
- What is JSESSIONID in Java Web application? (answer)
- How many bean scope is available in Spring framework? (answer)
- Difference between Setter and Constructor injection in Spring? (answer)
- What is difference between Web and Application
Server ? (answer) - Difference between GET and POST HTTP request? (answer)
- What is difference between URL encoding and URL Rewriting? (answer)
- Difference between include directive and include action in JSP? (answer)
- How to define application wide error page in JSP? (answer)
- What is difference between ServletConfig and ServletContext? (answer)
- How to
escape XML metacharacters in JSP? (answer)
No comments:
Post a Comment