1. What is difference between GenericServlet and HttpServlet?
Generic Servlet:
GenericServlet class is direct subclass of Servlet interface.
Generic Servlet is protocol independent.It handles all types of protocol like http, smtp, ftp etc.
Generic Servlet only supports service() method.It handles only simple request
public void service(ServletRequest req,ServletResponse res ).
Generic Servlet only supports service() method.
HttpServlet:
HttpServlet class is the direct subclass of Generic Servlet.
HttpServlet is protocol dependent. It handles only http protocol.
HttpServlet
supports public void service(ServletRequest req,ServletResponse res )
and protected void service(HttpServletRequest req,HttpServletResponse
res).
HttpServlet supports also doGet(),doPost(),doPut(),doDelete(),doHead(),doTrace(),doOptions()etc.
2. What is a deployment descriptor?
A
web application's deployment descriptor describes the classes,
resources and configuration of the application and how the web server
uses them to serve web requests. When the web server receives a request
for the application, it uses the deployment descriptor to map the URL of
the request to the code that ought to handle the request.
The
deployment descriptor is a file named web.xml. It resides in the app's
WAR under the WEB-INF/ directory. The file is an XML file whose root
element is <web-app>.
3. What is ServletConfig object?
An
object of ServletConfig is created by the web container for each
servlet. This object can be used to get configuration information from
web.xml file.
If the configuration information is modified from the
web.xml file, we don't need to change the servlet. So it is easier to
manage the web application if any specific content is modified from time
to time.
4. What is ServletContext object?
An
object of ServletContext is created by the web container at time of
deploying the project. This object can be used to get configuration
information from web.xml file. There is only one ServletContext object
per web application.
If any information is shared to many servlet, it
is better to provide it from the web.xml file using the
<context-param> element.
5. What is difference between ServletConfig and ServletContext?
ServletConfig
ServletConfig available in javax.servlet.*; package
ServletConfig object is one per servlet class
Object of ServletConfig will be created during initialization process of the servlet
This Config object is public to a particular servlet only
Scope:
As long as a servlet is executing, ServletConfig object will be
available, it will be destroyed once the servlet execution is completed.
We should give request explicitly, in order to create ServletConfig object for the first time
In web.xml ? <init-param> tag will be appear under <servlet-class> tag
ServletContext
ServletContext available in javax.servlet.*; package
ServletContext object is global to entire web application
Object of ServletContext will be created at the time of web application deployment
Scope:
As long as web application is executing, ServletContext object will be
available, and it will be destroyed once the application is removed from
the server.
ServletContext object will be available even before giving the first request
In web.xml ? <context-param> tag will be appear under <web-app> tag
So finally??.
No. of web applications = That many number of ServletContext objects [ 1 per web application ]
No. of servlet classes = That many number of ServletConfig objects
6. What is RequestDispatcher?
The
RequestDispatcher interface provides the facility of dispatching the
request to another resource it may be html, servlet or jsp. This
interface can also be used to include the content of another resource
also. It is one of the way of servlet collaboration.
7. What is the interservlet communication?
Request Dispatching
HTTP Redirect
Servlet Chaining
HTTP request (using sockets or the URLConnection class)
Shared session, request, or application objects (beans)
Direct method invocation (deprecated)
Shared static or instance variables (deprecated)
Search
the FAQ, especially topic Message Passing (including Request
Dispatching) for information on each of these techniques. -Alex]
Basically
interServlet communication is acheived through servlet chaining. Which
is a process in which you pass the output of one servlet as the input to
other. These servlets should be running in the same server.
e.g.
ServletContext.getRequestDispatcher(HttpRequest,
HttpResponse).forward("NextServlet") ; You can pass in the current
request and response object from the latest form submission to the next
servlet/JSP. You can modify these objects and pass them so that the next
servlet/JSP can use the results of this servlet.
8. How do we call one servlet from another servlet?
We
can use RequestDispatcher forward() method to forward the processing of
a request to another servlet. If we want to include the another servlet
output to the response, we can use RequestDispatcher include() method.
public class Populate_ServletName extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
//Insert records
//Print confirmation
}
9. How can we invoke another servlet in a different application?
We
can?t use RequestDispatcher to invoke servlet from another application
because it?s specific for the application. If we have to forward the
request to a resource in another application, we can use ServletResponse
sendRedirect() method and provide complete URL of another servlet. This
sends the response to client with response code as 302 to forward the
request to another URL. If we have to send some data also, we can use
cookies that will be part of the servlet response and sent in the
request to another servlet.
10. What is difference between ServletResponse sendRedirect() and RequestDispatcher forward() method?
RequestDispatcher
forward() is used to forward the same request to another resource
whereas ServletResponse sendRedirect() is a two step process. In
sendRedirect(), web application returns the response to client with
status code 302 (redirect) with URL to send the request. The request
sent is a completely new request.
forward() is handled internally by the container whereas sednRedirect() is handled by browser.
We
should use forward() when accessing resources in the same application
because it?s faster than sendRedirect() method that required an extra
network call.
In forward() browser is unaware of the actual
processing resource and the URL in address bar remains same whereas in
sendRedirect() URL in address bar change to the forwarded resource.
forward() can?t be used to invoke a servlet in another context, we can only use sendRedirect() in this case.