when we override the init method of servlet we give a call to super.init(ServletConfig). can somebody please elaborate why do we need to do this. What actually happens when we call the super.init method I even want to know why do we need to use a servletconfig object to store the init parameters and access these parameters by using the getServletConfig method in our service method.
hi, very intresting ?.Infact i also asked the same ? some time back about 2 months or so and i also got the answer but you also have to worked for it.just see the reply for my ? but for that you have to search the ?.good luck. asheet
In your deployment descriptor, there is a section for servlet parameters. When the servlet container loads the servlet, it reads those parameters and stores them in the ServletConfig object. You need a ServletConfig object because that is the object required by the Specification for the servlet container to store and pass init parameters to the servlet. However, you don't need to call getServletConfig() to get a ServletConfig object. You can call all the ServletConfig methods directly without a ServletConfig object. How is this possible? If you look at the ServletConfig class, you'll see that it's really just an interface. The ServletConfig object you get in init() is an object that implements ServletConfig. Also, if you look at the GenericServlet class, you'll see that GenericServlet also implements ServletConfig!! Why do both ServletConfig AND GenericServlet implement this interface? The interface ServletConfig provides methods for easily retrieving the init parameters for the servlet. Since all servlets have GenericServlet as a parent class, every servlet is guaranteed to have the methods for getting servlet parameters. When you call super.init(ServletConfig) the GenericServlet class stores the reference to the ServletConfig object. This allows you to call getInitParameter(String) directly in your servlet without a specific ServletConfig reference. That is, you don't need to write ServletConfig sc = getServletConfig(); String param = sc.getInitParameter(name); you can just write: String param = getInitParameter(name); The getInitParameter() call goes to the GenericServlet, where GenericServlet delegates the call to the ServletConfig object that was stored earlier. If you do not call super.init(ServletConfig), the config object is not stored, so any calls to ServletConfig methods would fail. Of course, if your servlet has not init parameters, and you never need any of the other methods of the ServletConfig interface, then it's safe to not call the super.init(ServletConfig) method.
Originally posted by sushant prabhu: when we override the init method of servlet we give a call to super.init(ServletConfig). can somebody please elaborate why do we need to do this. What actually happens when we call the super.init method I even want to know why do we need to use a servletconfig object to store the init parameters and access these parameters by using the getServletConfig method in our service method.
[This message has been edited by Kevin Mukhar (edited March 27, 2001).]