init(ServletConfig) is called by container and saves ServletConfig object for you. init() is a convenience method for developer for providing servlet initialization. However,you can override init(ServletConfig) but in that case you must include call to super.init(ServletConfig).
However,you can override init(ServletConfig) but in that case you must include call to super.init(ServletConfig).
Any reasons why?? I can just override init(ServletConfig) but why do I have to provide a call to super.init(ServletConfig)??
Saurabh Kumar
Ranch Hand
Joined: Aug 21, 2006
Posts: 56
posted
0
because until container calls init(ServletConfig), you won't get ServletConfig object which you need/use in servlet initialization like reading servlet initialization parameters from web.xml.
That's why there is other method for your convenience: init().
Thanks and regards, Saurabh
Ali Gohar
Ranch Hand
Joined: Mar 18, 2004
Posts: 572
posted
0
Jothi,
You are right you have the ServletConfig object when you override init(ServletConfig) because you can save it for further use.
But by overriding init(ServletConfig) you can't use getServletConfig() method and also you can't use getServletContext() method to get the servlet context instead you have to use request.getSession().getServletContext() to get the servlet context. Similarly you can't use getInitParameter("param") to get the initialization parameters.
"But by overriding init(ServletConfig) you can't use getServletConfig() method and also you can't use getServletContext() method to get the servlet context instead you have to use request.getSession().getServletContext() to get the servlet context. Similarly you can't use getInitParameter("param") to get the initialization parameters."
- It calls the init() method from it which you override in your servlet - It saves ServletConfig object and ServletContext object for you in instance variables and you can get them using getServletConfig() and getServletContext() methods
- Similarly GenericServlet provides you a utility method to get init params which is getInitParams(param) [ March 06, 2007: Message edited by: Ali Gohar ]