Home / Interview / Servlets :: General Questions

Interview :: Servlets

1) How many objects of a servlet is created?

Only one object at the time of first request by servlet or web container.

2) What is the life-cycle of a servlet?
  1. Servlet is loaded
  2. servlet is instantiated
  3. servlet is initialized
  4. service the request
  5. servlet is destroyed
3) What are the life-cycle methods for a servlet?
MethodDescription
public void init(ServletConfig config)It is invoked only once when first request comes for the servlet. It is used to initialize the servlet.
public void service(ServletRequest request,ServletResponse)throws ServletException,IOException It is invoked at each request.The service() method is used to service the request.
public void destroy()It is invoked only once when servlet is unloaded.
4) Who is responsible to create the object of servlet?

The web container or servlet container.

5) When servlet object is created?

At the time of first request.

6) What is difference between Get and Post method?
GetPost
1) Limited amount of data can be sent because data is sent in header.Large amount of data can be sent because data is sent in body.
2) Not Secured because data is exposed in URL bar.Secured because data is not exposed in URL bar.
3) Can be bookmarkedCannot be bookmarked
4) IdempotentNon-Idempotent
5) It is more efficient and used than PostIt is less efficient and used
7) What is difference between PrintWriter and ServletOutputStream?

PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information whereas ServletOutputStream class can be used to write primitive values as well as character-based information.

8) What is difference between GenericServlet and HttpServlet?

The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific. HttpServlet provides additional functionalities such as state management etc.

9) What is servlet collaboration?

When one servlet communicates to another servlet, it is known as servlet collaboration. There are many ways of servlet collaboration:

  • RequestDispacher interface
  • sendRedirect() method etc.
10) What is the purpose of RequestDispatcher Interface?

The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp. This interceptor can also be used to include the content of antoher resource.