Collection of large number of Servlet Interview Questions. These questions are frequently asked in the Java Interviews.
Collection of large number of Servlet Interview Questions. These questions are frequently asked in the Java Interviews.Collection of large number of Servlet Interview Questions. These questions are frequently asked in the Java Interviews.
Question: What is a Servlet?
Answer: Java Servlets are server side components that provides a powerful
mechanism for developing server side of web application. Earlier CGI was
developed to provide server side capabilities to the web applications. Although
CGI played a major role in the explosion of the Internet, its performance,
scalability and reusability issues make it less than optimal solutions. Java
Servlets changes all that. Built from ground up using Sun's write once run
anywhere technology java servlets provide excellent framework for server side
processing.
Question: What are the types of Servlet?
Answer: There are two types of servlets, GenericServlet and HttpServlet.
GenericServlet defines the generic or protocol independent servlet.
HttpServlet is subclass of GenericServlet and provides some http specific
functionality linke doGet and doPost methods.
Question: What are the differences between
HttpServlet and Generic Servlets?
Answer: HttpServlet Provides an abstract class to be
subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet
must override at least one method, usually one of these:
doGet
, if the servlet supports HTTP GET requestsdoPost
, for HTTP POST requestsdoPut
, for HTTP PUT requestsdoDelete
, for HTTP DELETE requestsinit
and destroy
, to manage resources that are
held for the life of the servletgetServletInfo
, which the servlet uses to provide information
about itselfThere's almost no reason to override the service
method. service
handles standard HTTP requests by dispatching them to the handler methods for
each HTTP request type (the do
XXX methods listed above).
Likewise, there's almost no reason to override the doOptions
and doTrace
methods.
GenericServlet defines a generic, protocol-independent servlet. To
write an HTTP servlet for use on the Web, extend HttpServlet
instead.
GenericServlet
implements the Servlet
and ServletConfig
interfaces. GenericServlet
may be directly extended by a servlet,
although it's more common to extend a protocol-specific subclass such as HttpServlet
.
GenericServlet
makes writing servlets easier. It provides simple
versions of the lifecycle methods init
and destroy
and
of the methods in the ServletConfig
interface. GenericServlet
also implements the log
method, declared in the ServletContext
interface.
To write a generic servlet, you need only override the abstract service
method.
Question: Differentiate between Servlet and
Applet.
Answer: Servlets are server side components that executes on the
server whereas applets are client side components and executes on the web
browser. Applets have GUI interface but there is not GUI interface in case of
Servlets.
Question: Differentiate between doGet and doPost
method?
Answer: doGet is used when there is are requirement of sending data
appended to a query string in the URL. The doGet models the GET method of Http
and it is used to retrieve the info on the client from some server as a request to it.
The doGet cannot be used to send too much info appended as a query stream. GET puts the form values into the URL string. GET is limited to about 256 characters (usually a browser limitation) and creates really ugly URLs.
POST allows you to have extremely dense forms and pass that to the server without clutter or limitation in size. e.g. you obviously can't send a file from the client to the server via GET. POST has no limit on the amount of data you can send and because the data does not show up on the URL you can send passwords. But this does not mean that POST is truly secure. For real security you have to look into encryption which is an entirely different topic
Question: What are methods of HttpServlet?
Answer: The methods of HttpServlet class are :
* doGet() is used to handle the GET, conditional GET, and HEAD requests
* doPost() is used to handle POST requests
* doPut() is used to handle PUT requests
* doDelete() is used to handle DELETE requests
* doOptions() is used to handle the OPTIONS requests and
* doTrace() is used to handle the TRACE requests
Question: What are the advantages of Servlets
over CGI programs?
Answer: Question: What are methods of HttpServlet?
Answer: Java Servlets have a number of
advantages over CGI and other API's. They are:
Ads