Servlet interview questions

Collection of frequently asked interview questions from Servlet.

How can we create a Servelt?

A Servlet can be created by extending the httpServlet or GenericServlet classes.

Explaing what you mean by servlets?

Servlets are server side components that provide a powerful mechanism for developing server side programs. Servlets provide component-based, platform-independent methods for building Web-based applications, without the performance limitations of CGI programs.

Explain the life cycle methods of a Servlet?

Servlet interface contains three methods known as life-cycle methods.
Init , service and destroy.
First the servlet is constructed, then initialized with the init() method. Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet. The servlet is removed from service, destroyed with the destroy() method, then garbaged collected and finalized.

Explain the common ways used for session tracking?

SSL sessions
URL- rewriting

Explain the difference between HttpServlet and GenericServlet?

A GenericServlet has a service() method to handle the requests. HttpServlet extends GenericServlet and adds support for doGet (), doPost () methods doPut (), doOptions (), doDelete (), doTrace () methods.

Explain the difference between ServletConfig and ServletContext?

ServletContext: It defines a set of methods that a servlet uses to communicate with its container.
ServletConfig: The object created after a servlet is instantiated and its default constructor is read. It is created to pass initialization information to the servlet.

Explain what is a  session?

The session is an object used by a servlet to track a user’s interaction with a Web application across multiple HTTP requests. The session is stored on the server.