Servlet interview Questions - 2

1- What is a Servlet?
Java Servlets is a Server side technology(basically specification) that allow the programmer to develop dynamic web resource program or server side web resource program in Java based web application.

Servlet is the software specification given by sun microsystem that provide set of rule and guideline for vendor company to develop software called servlet container.

Servlet is single instance multiple threads based java component in Java web application to generate dynamic web application.

2- What are the types of Servlet?
There are two types of servlets, GenericServlet and HttpServlet. GenericServlet defines the generic or protocol independent servlet. HttpServlet is subclass of GenericServlet and provides http protocl specific functionality.

3- What are the differences between a Servlet and an Applet? 
1) Servlets are server side components that executes on the server whereas applets are client side components and executes on the web browser. 

2)Applets have GUI interface but there is no GUI interface in case of Servlets. 

3)Applets have limited capabilities whereas Servlets are very poweful and can support a wide variety of operations 

4)Servlets can perform operations like interacting with other servlets, JSP Pages, connect to databases etc while Applets cannot do all this. 

4- What are the different methods present in a HttpServlet?
The methods of HttpServlet class are :
doGet() - To handle the GET, conditional GET, and HEAD requests

doPost() - To handle POST requests

doPut() - To handle PUT requests

doDelete() - To handle DELETE requests

doOptions() - To handle the OPTIONS requests and

doTrace() - To handle the TRACE requests

Apart from these, a Servlet also contains init() and destroy() methods that are used to initialize and destroy the servlet respectively. They are not any operation specific and are available in all Servlets for use during their active life-cycle.

5- What are the advantages of Servlets over CGI programs?
Java Servlets have a number of advantages over CGI and other API's. Some are:

1. Platform Independence - Java Servlets are 100% pure Java, so it is platform independent. It can run on any Servlet enabled web server. For example if you develop an web application in windows machine running Java web server. You can easily run the same on apache web server without modification code. Platform independency of servlets provide a great advantages over alternatives of servlets.

2. Performance - Anyone who has used CGI would agree that Servlets are much more powerful and quicker than CGI. Because the underlying technology is Java, it is fast and can handle multiple request simultaneously. Also, a servlet gets initialized only once in its lifetime and then continues to serve requests without having to be re-initialized again, hence the performance is much higher than CGIs.

3. Extensibility - Java Servlets are developed in java which is robust, well-designed and object oriented language which can be extended or polymorphed into new objects. So the java servlets takes all these advantages and can be extended from existing class the provide the ideal solutions.

Also, in terms of Safety & Security Servlets are superior when compared to CGI.

6- What are the type of protocols supported by HttpServlet?
It extends the GenericServlet base class and provides an framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol.

7- What is ServletContext?
ServletContext is an Interface that defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine.

8- What is meant by Pre-initialization of Servlet?
When servlet container is loaded, all the servlets defined in the web.xml file do not get initialized by default. When the container receives a request to hit a particular servlet, it loads that servlet. But in some cases if you want your servlet to be initialized when context is loaded, you have to use a concept called pre-initialization of Servlet. In this case, the servlet is loaded when context is loaded. You can specify 1 in between the tag in the Web.xml file in order to pre-initialize your servlet.



9- What mechanisms are used by a Servlet Container to maintain session information?
Servlet Container uses Cookies, URL rewriting, and HTTPS protocol information to maintain the session.

10- What do you understand by servlet mapping?
Servlet mapping defines an association between a URL pattern and a servlet. You can use one servlet to process a number of url patterns. For example in case of Struts *.do url patterns are processed by Struts Controller Servlet.

11- What interface must be implemented by all Servlets?
The Servlet Interface must be implemented by all servlets (either the GenericServlet or the HttpServlet)

12- What are the uses of Servlets?
1) Servlets are used to process the client requests.

2) A Servlet can handle multiple request concurrently and be used to develop high performance system
3) A Servlet can be used to load balance among serveral servers, as Servlet can easily forward request.

13- What are the objects that are received when a servlets accepts call from client? 
The objects are:
ServeltRequest and

The ServeltRequest encapsulates the communication from the client to the
server. While ServletResponse encapsulates the communication from the Servlet back to the client. All the passage of data between the client and server happens by means of these request and response objects.