MY mENU


Saturday, 17 March 2012

Servlet Questions-part3


How can we get ServletConfig object ?
1st way :
public class TestServlet extends HttpServlet
{
public void init(ServletConfig cg)
{
super.init(cg);
===== usage of cg here ======
}
}

2nd way :
public class TestServlet extends HttpServlet
{
public void init(ServletConfig cg)
{
ServletConfig cg=getServletConfig();
==== use cg here=====
}
}

Note : getServletConfig() is the public method avaialbe in GenericServlet class.

How can we creae ServletContext object ?
First way :
public class TestServlet extends HttpServlet
{
public void service(HttpServletRequest req ,HttpServletResponse response) throws ServletException, IOException
{
ServletConfig cg=getServletConfig();
ServletContext sc=cg.getServletContext();
====== usage of ServletContext object here ====
}
}

Second way :
public class TestServlet extends HttpServlet
{
public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException ,IOException
{
ServletConfig sc=getServletContext();
====== usage of ServletContext object here ====
}
}
Note : getServletContext() is public method available in javax.servlet.ServletConfig interface and 
javax.servlet.GenericServlet class.

When ServletConfig object will be create ? when it will be destroy ? What is the use?
ServletConfig object will be created along with servlet object& will be destroyed along with servlet object. When container creates, object of servlet it uses zero argument constructor. “ServletConfig” object is right hand object for servlet object”. To pass additional info to servlet or to gather info about servlet object programmer uses ServletConfig object.

When ServletContext object will be create?when it will be destroy ?what is the use ?
web-container creates ServetContext object when web-application is deployed. And destroys ServletContext object when web-application stopped (or) reloaded (or) undeployed.

There is a web server having 10 java based web applications out of these 10 web applications only 4 web-applications are in running mode ,6 web-applications are in stopped mode can you tell me how many ServletContext objects are currently available?  Since ServeltContext object will be destroyed when web-application goes to stopped mode there will be only 4 number of ServletContext objects.

There is a web-application deployed in a web server having 10 servlets .out of 10 servlets 3 servlets load-on-startup is enabled and other 3 servlets are requested from client. Can you tell me total how many servletConfig objects are available currently in the web application?
Since ServletConfig object will be created only when servlet object will be created there will be total 6 number of ServletConfig objects[3 for requested servlet objects,3 for other srevlets on which load-on-startup is enabled].
Note : ServletConfig object will not be created HTML and image web resources. Even though web application is created without servlets there will be ServletContext object for that web application.web container creates ServletContext object before creating any servlet object.
Why super.init (config) will be the first statement inside init(ServletConfig config) method.
This will be the first statement if we are overriding the init(config ) method by this way we will store the config object for future reference and we can use by getServletConfig() to get information about config object. if we not do this config object will be lost and we have only one way to get config object because servlet pass config object only in init method . Without doing this if we call the servletConfig method will get NullPointerException.

What's the difference between init() & init(ServletConfig) and Which is better ?
It is Better to use init(). If you use init() you have no such worries as calling super.init(config).
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.

What is wrong if i don't call super(ServletConfig) in the init(ServletConfig) ?
You may create a subclass which extends GenericServlet class. If you override init(ServletConfig) method and don't call super(servletConfig) then the ServletConfig reference is not stored in the GenericServlet and if we call getServletConfig() we get null. The reason we're advised to add super.init(config) is because GenericServlet.init(ServletConfig) stores a copy of the ServletConfig so that its getInitParameter() can forward the call to the ServletConfig object. If you override init(ServletConfig) in your own code you should add super.init(config) to preserve that functionality.

Another method of GenericServlet, init() is provided as a convenience. GenericServlet.init(ServletConfig) internally calls GenericServlet.init(). If you want to add functionality at init-time, you should do it by overriding init(). The default implementation of init(ServletConfig) will save the ServletConfig reference and then call your overriding init().

Can we override service(request,response) in HttpServlet ?
You can override service method in HttpServlet also.But not recommended. If you override service method in HttpServlet then call will go to service() method instead of doGet() or doPost() method. But this is not good practise. If the jsp form you mentioned

Then also call will go to service method of the servlet. Call don't go to doPost() method. you can call doPost() method explicitly from servive() method.
If the jsp form you mentioned

Now also call will go to service method of the servlet. Call don't go to doGet() method. you can call doGet () method explicitly from servive() method.

what is servlet collaboration?
(OR)
In how many ways two servlets can communicate?
communication between two servlet is called servlet collaboration which is achieved by 3 ways.
1. RequestDispatchers include () and forward() method .
2. Using SendRedirect()method of Response object.
3. Shared static or instance variables (deprecated)

No comments:

Post a Comment