Do we have a constructor in servlet ? can we explictly provide a constructor in servlet programme as in java program ?

Showing Answers 1 - 51 of 51 Answers

Sureshbabu S

  • Jun 28th, 2005
 

Answer We can have a constructor in servlet .

Asohan

  • Jul 9th, 2005
 

Brefily need answer about constructor in servlet . when it execute 

  Was this answer useful?  Yes

smarajeet

  • Jul 13th, 2005
 

need at what senario we will use the constructor in servlet.

  Was this answer useful?  Yes

Venkat

  • Jul 13th, 2005
 

Can we call "destroy" method from "init" method()? 
If so what will happen?

  Was this answer useful?  Yes

Bharat Kumawat

  • Jul 21st, 2005
 

One can definitely have constructor in servlet.Even you can user the constrctor in servlet for initialization purpose,but this type of approch is not so common.You can perform common operations with the constructor as you normally do.The only thing is that you cannot call that constructor explicitly by the new keyword as we normally do.In the case of servlet, servlet container is responsible for instentiating the servlet, so the constructor is also called by servlet container only.

  Was this answer useful?  Yes

Vishal

  • Aug 11th, 2005
 

Cetainly , we can have a constructor in servlet class, but only default constructor. We can not have parametrized constructor in servlet, because the servlet gets instantiated by the container dynamically, and java does not allow to call parametrized constuctor for dynamically loaded clases.

  Was this answer useful?  Yes

karan

  • Aug 24th, 2005
 

constructor is desault in servlets. We can write constructor for the servlet. But there is a problem with the initalization of parameters. 
So we don't provide initilization parameters  
with in this constructor.

  Was this answer useful?  Yes

sureshlg

  • Dec 1st, 2005
 

u can call destroy() method inside init() method nothieng happend

bzc destroy() method called by contanier, we enable to call our own

  Was this answer useful?  Yes

Xavier Leon

  • Apr 27th, 2006
 

In Which senario can we create a constructor for the Servlets

  Was this answer useful?  Yes

sakthivel

  • Jul 14th, 2006
 

Constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments. So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface. Also, Java doesn't allow interfaces to declare constructors. This means that the javax.servlet.Servlet interface cannot declare a constructor that accepts a ServletConfig parameter. It has to declare another method, like init(). It's still possible, of course, for you to define constructors for your servlets, but in the constructor you don't have access to the ServletConfig object or the ability to throw a ServletException.This ServletConfig object supplies a servlet with information about its initialization (init) parameters. These parameters are given to the servlet itself and are not associated with any single request. They can specify initial values, such as where a counter should begin counting, or default values, perhaps a template to use when not specified by the request. Hope this explaination solves your doubt.
Cheers,
Sakthi

  Was this answer useful?  Yes

kiran.vennampelli

  • Dec 15th, 2006
 

They can have a constructor.But you never call the constructor for the servlet / instantiate them , 'cos the container handles it.So you are better of doing initialization / one-time setup code in the init method of the servlet. Also, the container passes the ServletConfig object to the servlet only when it calls the init method. So ServletConfig will not be accessible in the constructor.However, you might still need the init parameters, so initialization is done in init instead of a constructor.

  Was this answer useful?  Yes

Deepak Sharma

  • Dec 20th, 2006
 

i also want to know, where we should use constructor in servlet and why to need it .

thanx:

Deepak sharma

mail me: deepak.btg@gmail.com

  Was this answer useful?  Yes

rameshvdnv

  • Apr 25th, 2007
 

Yes, we can have a constructor in aservlet class just write the default constructor in aservlet class which implements HTTPServlet and when u execute u will be able to know that the servlet object created by the container in the server.

  Was this answer useful?  Yes

satya

  • Sep 28th, 2007
 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* @version 1.0

* @author

*/

public class MyServlet extends HttpServlet {

/**

* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

*/

MyServlet(){

System.out.println(
"This is inside the servlet constructor");

}

public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

out.println("Welcome to this application");

out.println("get Free Java,J2EE,Struts,XML EBooks and tutorials HErE");
out.println("<a href="http://books4java.blogspot.com">CLiCK HERE</a>");
}

}



[Servlet Error]-[MyServlet]: Failed to load servlet: java.lang.ClassNotFoundException: class com.test.MyServlet : java.lang.IllegalAccessException: Class can not access a member of class com.test.MyServlet with modifiers "".

meaning that we can't define constuctors inside the servlet ,even the Default Constructor.

  Was this answer useful?  Yes

Servlet default constructor is call in init method we can define a argumrnted constructor  try this code .......
and aslo we can intialize variable in constructor


/*
 * MyServlet.java
 *
 * Created on November 26, 2007, 4:43 PM
 */

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;




/**

* @version 1.0

* @author

*/
public class MyServlet extends HttpServlet {

/**

* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

*/
int a;
public MyServlet(){
    a=10;
System.out.println("This is inside the servlet constructor");

}
public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();
out.println("Welcome to this application"+a);

out.println("get Free Java,J2EE,Struts,XML EBooks and tutorials HErE");

}

}



  Was this answer useful?  Yes

Servlets are dynamically laoded Java classes such classes cannot have constructors with parameters.So even if you declare a constructor for servlet you cant pass any parameter and if you cant pass any parameter then you cant have servletconfig object in ur constructor.

 So there is not very much benefit in having a constructor in servlet but yes you can declare constructors.

  Was this answer useful?  Yes

yogi4uwar

  • Dec 15th, 2008
 

Yes. we can have a constructor in Servlet. we can also explictly provide a constructor in servlet programme as in java program. If we are not providing also it will take the default constructor with empty parameters.

  Was this answer useful?  Yes

Yes we can provide a constructor in servlet. Even we can use it to initialize any variable. But we can't replace it with init (servletconfig) method of servlet. As servlet is dynamically loaded by the container it is not possible to call your constructor. It will call its own default constructor init(). from that it will call init(servletconfig) to provide servletness to your servlet.

  Was this answer useful?  Yes

sharathe

  • Mar 24th, 2009
 

Yes apart from the default constructor we can provide non-default constructor in the servlet. But the container only invokes the default constructor, so whatever construct you provide you have to invoke it from the default constructor.

  Was this answer useful?  Yes

Yes we can have a constructor in servlet.But it can't be called .Coz servlets are dynamically loded classes and dynamically loded classes can't  have constructor with argument.it can only have default constructor without any argument.
But our servlet needs parametrised constructor that has ServletConfig as reference variable.Object of this type provides servlet information about its environment to the servlet which is needed by any servlet as soon as it gets loaded.
Now when the servlet loads in the memory our container calls Init() method of javax.servlet.Servlet interface which internally calls Init(ServletConfig) argument.
so even if u are writinh ur cinstructor inside init() it wont be called.

  Was this answer useful?  Yes

surikvs332

  • Jun 22nd, 2010
 

We can have constructor in the servlet but no use of it. We are initalizing all the parameters in the INIT method  of serlet.  Ancient versions of java will not invoke the constructors with  arguments. From constructor you will not access the serlet context and serlet config.

  Was this answer useful?  Yes

Technically you can define constructors in servlet. But, the declared constructor cannot access the ServletConfig object or throw a ServletException so placing a constructor wouldn't add much value and hence we usually don't do it.

Then why is it not customary to declare a constructor in a servlet? Because the init() method is used to perform servlet initialization. In JDK 1.0 (servlet were written in this version), constructors for dynamically loaded Java classes such as servlets cannot accept arguments. Also, Java constructors cannot be declared in interfaces and javax.servlet.Servlet is an interface. Now the classes which implement this interface are GenericServlet and HttpServlet which don't do anything in these constructors [as per the API documentation].

So, javax.servlet.Servlet interface cannot have a constructor that accepts a ServletConfig parameter. To overcome this, init() method is used for initialization instead of declaring a constructor.


Thanks,
Vinay

  Was this answer useful?  Yes

Servlet container creates object for our class when the default constructor is there in our class..if not provide then container will take care about that...

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions