*Filter candidates using TestYourCandidate.com and save 80% time ( saving time for recruiters and interviewers)
*More than 50% candidates can be filtered on Candidate Screening online tests
*Inbuilt exams are available in library, created and tested by experts Try Today...
Q.Why don't we write a constructor in a servlet? view answer
Q.When we don't write any constructor for the servlet, how does container create an instance of servlet? view answer
Q.Why is it that we can't give relative URL's when using ServletContext.getRequestDispatcher() when we can use the same while calling ServletRequest.getRequestDispatcher()? view answer
Q.Once the destroy() method is called by the container, will the servlet be immediately destroyed? What happens to the tasks(threads) that the servlet might be executing at that time? view answer
Q.Request parameter How to find whether a parameter exists in the request object?
view answer
What is new in ServletRequest interface ? view answer
Q.Given the request path below, which are context path, servlet path and path info?
/bookstore/education/index.html view answer
Q.When a client request is sent to the servlet container, how does the container choose which servlet to invoke? view answer
Before start we have to understand the servlet flow.
For example you have servlet LoginServlet which extends HttpServlet
public class LoginServlet extends HttpServlet{
}
And your HttpServlet internally extends GenericServlet.
public abstract class GenericServlet
implements Servlet, ServletConfig, Serializable
{
public GenericServlet()
{
}
public void init()
throws ServletException
{
}
public ServletConfig getServletConfig()
{
return 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.