First you have to understand the bellow code for clear understanding,initially create a servlet class(user defined) extend from HttpServlet but HttpServlet internally extends a Generic Servlet
}
------------------------------------------------
public abstract class GenericServlet
implements Servlet, ServletConfig, Serializable
{
public GenericServlet()
{ }
public void init() throws ServletException
{ }
public ServletConfig getServletConfig()
{ return config;}
public void init(ServletConfig config) throws ServletException
{
this.config = config;
init();
}
public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
throws ServletException, IOException;
private transient ServletConfig config;
}
Process of Execution:
Step 1. Loads the servlet class and create instance of the servlet class (LoginServlet).
LoginServlet login = new LoginServlet();
Step 2. Then servlet container create ServletConfig object for that servlet and
Call login.init(ServletConfig);
Case 1 :
If you have overridden init(ServletConfig) method in your servlet then call goes to your
init(ServletConfig) method .
public void init(ServletConfig config) throws ServletException
{
System.out.println("\n**** Initializing LoginServlet Init Servlet ********** \n");
super.init(config);
}
It will print "Initializing LoginServlet Init Servlet" and call goes to supper class GenericServlet init(ServletConfig) method.
In the GenericServlet init(ServletConfig) method there is code
This.config= config // initialize the Servlet config object and it is available to you.
Case 2 :
If you overridden init() method and not overridden init(ServletConfig) method.
Servlet container call login.init(ServletConfig);
There is no method like init(ServletConfig) in your servlet so call directly goes to super class GenericServlet init(ServletConfig) method.
This.config= config // initialize the Servlet config object and it is available to you.
You can get the Servlet config object using getServletConfig() method.
Conclusion: It is BETTER to use init(). If you use init() you have no such worries as calling super.init().
If you use init(servletconfig) and forgot to call super.init(config) then servletconfig object will not set and you will not get the servletconfig object.
public
class LoginServlet extends HttpServlet
{}
------------------------------------------------
public abstract class GenericServlet
implements Servlet, ServletConfig, Serializable
{
public GenericServlet()
{ }
public void init() throws ServletException
{ }
public ServletConfig getServletConfig()
{ return config;}
public void init(ServletConfig config) throws ServletException
{
this.config = config;
init();
}
public abstract void service(ServletRequest servletrequest, ServletResponse servletresponse)
throws ServletException, IOException;
private transient ServletConfig config;
}
Process of Execution:
Step 1. Loads the servlet class and create instance of the servlet class (LoginServlet).
LoginServlet login = new LoginServlet();
Step 2. Then servlet container create ServletConfig object for that servlet and
Call login.init(ServletConfig);
Case 1 :
If you have overridden init(ServletConfig) method in your servlet then call goes to your
init(ServletConfig) method .
public void init(ServletConfig config) throws ServletException
{
System.out.println("\n**** Initializing LoginServlet Init Servlet ********** \n");
super.init(config);
}
It will print "Initializing LoginServlet Init Servlet" and call goes to supper class GenericServlet init(ServletConfig) method.
In the GenericServlet init(ServletConfig) method there is code
This.config= config // initialize the Servlet config object and it is available to you.
Case 2 :
If you overridden init() method and not overridden init(ServletConfig) method.
Servlet container call login.init(ServletConfig);
There is no method like init(ServletConfig) in your servlet so call directly goes to super class GenericServlet init(ServletConfig) method.
This.config= config // initialize the Servlet config object and it is available to you.
You can get the Servlet config object using getServletConfig() method.
Conclusion: It is BETTER to use init(). If you use init() you have no such worries as calling super.init().
If you use init(servletconfig) and forgot to call super.init(config) then servletconfig object will not set and you will not get the servletconfig object.

No comments:
Post a Comment