Servlet Interview Questions and Answers

  • By
  • November 18, 2019
  • JAVA Programming
Servlet Interview Questions and Answers

1.  What is Servlet?

– Java Servlet is server-side technologies to enhance the capability of web servers and ensuring support for dynamic response and data Storage.

2.  Comparison between the web server and application server?

  • A web server is a device or application program which handles HTTP request from a client browser and response to client client
  • Application Servers handles request and responses and also provide additional features such as Enterprise JavaBeans support, JMS Messaging support, Transaction Management

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

3.  Difference between CGI and Servlet?

Servlet CGI
Servlets can link directly to the Web server. CGI cannot directly link to Web server.
Servlets can share data among each other. CGI does not provide sharing property.
Servlets has the facility of performing session tracking and caching of previous computations. In CGI session tracking and caching is not done.
Servlet every request is processed by thread which is lightweight. In CGI,each request is handled   by  a  process which is heavy weighted.
Servlets are portable. CGI is not portable.
Servlet has session management techquies such as handle cookies,reading HTTP headers and analysing sessions, In CGI we do not have such techniques to manage sessions.
Servlets is inexpensive than CGI. CGI is more expensive than Servlets.


4. What is called a servlet container?

– A servlet container is a part of Web server which processes the request and generates the response. It also handles the servlets in the life cycle development.

5. What is a web application and what is its directory structure?

A web application is an application which runs on web client. A web application consists of web components like Servlet, JSP, Filter etc. alongwith HTML. For executing web components we need to install Web Server and process  HTTP request. Java provides web application support through Servlets and JSPs that can run in a servlet container and provide dynamic content to client browser.


6. Difference between doGet() and DoPost() methods?


Dif 1

Dif 2
Dif 2


7. Explain Life Cycle of Servlet?

  1. Loading Servlet Class :
  • Whenever first request for the servlet is received by the Web Server,servlet class is loaded into memory.
  1. Servlet instance creation :
  • After loading the servlet class,the instance of servlet is created by web container. Servlet instance is

created only once in the life cycle.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

  1. Call to the init() method :
  • In order to initialize the servlet,init() method is called by the Web Container i.e webServer.
  • Syntax: public void init(ServletConfig config) throws ServletException.
  • Call to the init method is done only once and not called again for each user request.
  1. Call to the service() method :
  • Call to the service() method is done each time when the request for servlet is received.
  • The service() method will then call the doGet, doPost, doPut, doDelete, etc. methods as

appropriate based on the type of the HTTP request.

  • Syntax: public void service(ServletRequest request, ServletResponse response) throws

ServletException, IOException

  • Web Server calls the service() method and service method in turn then calls the doGet, doPost,

doPut, doDelete, etc. methods as appropriate. So we override either doGet() or doPost()

depending on what type of request we receive from the client.

  1. Call to destroy() method:
  • The Web Container calls the destroy() method before removing servlet instance for cleanup


  • Syntax: public void destroy() {

// Finalization code…


  • It is called only once at the end of life cycle
  • Then it is marked for garbage collection

8. When init() method of servlet gets called?

  • The init method is created to be called only once. When servlet is first loaded into memory,that time the call goes to the init method,and just because it is loaded once so it is not called again for each user request.
  1. When service() method of servlet gets called?
  • After the initialization of Servet instance, the web server receives a request for a servlet, the server then calls service method. The service() method calls doGet, doPost, doPut, doDelete, etc. depending upon the type of HTTP Request.

10.When doGet() method of servlet to be called?

  • If while submiting a HTML form,no method is mentioned expilicility then call goes to the doGet()

11.When doPost() method of servlet to be called?

– A POST request comes from an HTML form when the form is submitted that specifically list POST as the METHOD and it should be handled by doPost() method which is called by service() method .

12.When destroy() method of servlet gets called?

  • The destroy() method is called only once at the end of the life cycle of a servlet because of one time object creation.

13. For what purpose init() method of a servlet is used?

  • The init() method is used to intialize the instance of Servlet which is created by Web Container.

14. For what purpose destroy() method of a servlet is used?

  • This method does all the cleanup activities as well as releases all resources use by servlet such as database connections, halt any background threads, write cookie lists or hit counts to disk.

15.What is session?Why it is used?

-Session is a conversation between a client and a server.A conversation comprises of multiple request and responses.

-HTTP is a stateless protocol. All requests and responses are independent. But sometimes we need tokeep track of client’s activity across multiple requests. For eg. When a User signup into a particular website,it does not matter on which web page he visits after logging in, his username,password or any any information should be with the server, until he logs out. So all this is managed by creating a session.

16.What is ServletConfig object?

  • servlet.ServletConfig is used to set and pass configuration information to a particular Servlet. Every servlet should create its own ServletConfig object and web container is responsible for instantiating this object. To set configuration information,we need to set it in web.xml file using <init-param> tag or through use of WebInitParam annotation.

17.What is ServletContext object?

  • ServletContext interface is present in javax.servlet package.
  • The ServletContext is consider as unique object and available to all the servlets within the web application.
  • When we want to set common parameters to be available to multiple or all of the servlets in the web application, we can use ServletContext object and define parameters in web.xml using <context- param> element.
  • We can create object of ServletContext via the getServletContext() method of ServletConfig.

18. What is Request Dispatcher?

Request Dispatcher interface is used to transfer the request to another resource that can be HTML, JSP or another servlet within the application. RequestDispatcher is also use to include the content of another resource to the response.

There are two methods defined in this interface:

  1. void forward(ServletRequest request, ServletResponse response) – forwards the request from a servlet to another resource (servlet, JSP file, or HTML file) on the
  2. void include(ServletRequest request, ServletResponse response) – includes the content of a resource (servlet, JSP page, HTML file) in the

Object of Request Dispatcher in a servlet is created using getRequestDispatcher (String path) method. The path must begin with a / and is refer to the current context root.

19.What are session Management Techniques?

Session Management is a mechanism used by the Web container to store everything that we can get from the client from all the requests the client makes. The basic concept behind the session is, whenever a user starts using our application, we can save a unique identification information about him, in an object which is available throughout the application, until its destroyed. Different techniques used by Servlet application for session  management are:

  1. HttpSession
  2. Cookies
  3. Hidden form field
  4. URL Rewriting

20)Difference between forward() and sendRedirect() method?

                forward() method sendRedirect() method
The forward() method works at server side. The sendRedirect() method works at client side.
It sends the same request and response

objects to another servlet.

It always sends a new request.
It can work within the application only. It can be used within and outside the application.



Example: response.sendRedirect(“servlet2”);

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

21) List Some methods of HttpSession?

                              Methods                   Description
long getCreationTime() returns the time when the session was created,measured in milliseconds since midnight January 1,1970 GMT.
String getId() returns a string containing the unique identifier assigned to the session.
long getLastAccessedTime() returns the time in seconds the client has lastly sent a request  associated with the session
int getMaxInactiveInterval() returns the maximum time interval  in seconds.
void invalidate() destroy the session
boolean isNew() returns true if the session  is new else false
void setMaxInactiveInterval(int interval) It specifies the time till what span the session will be expired.



22)What are the types of cookies?

There are 2 types of cookies in servlets.

  1. Non-persistent cookie
  2. Persistent cookie

Non-persistent cookie:

It is valid for single session only. It is destroy each time when user closes the browser.

Persistent cookie:

It is valid for multiple session . It is not destroy each time when user closes the browser. It is destroy only if user logout or signout.

Pallavi Kadam Software Trainer
SevenMentor Pvt Ltd.


Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *