Most frequently asked Servlet interview questions and answers to help you in interview preparation:
This series of Servlet questions will guide you through any Servlet interview.
It includes all about what a servlet is t, why Servlet came into the picture, the life cycle of Servlet and its methods, Load on start-up, WAR file, Deployment Descriptor, and its function, and the Welcome file and its importance.
We will also learn how Servlet works, Servlet Collaboration, Servlet config, Servlet context, Session tracking and techniques & benefits, Events, filter, its working principles, and its importance.
Servlet Interview Questions Quiz
Try this expert quiz to prove your Servlet skills. This quiz on Servlet interview questions covers fundamental and advanced concepts along with practical & scenario-based questions to ace your next interview.

The Interviewer always expects some real-time examples in your answer that turn out to be handy in interviews. Hence, if you go through all these questions thoroughly and if you can explain it in your own words, then you can get through an interview.
Best Servlet Interview Questions
Here is a quick list of some of the important questions covered in this article:
- What is Servlet?
- Types of a Servlet
- Servlet Life Cycle
- Servlet Life Cycle methods
- How Servlet Work?
- WAR file and its use?
- What is Load on Start-up and its importance?
- What is the deployment descriptor?
- Servlet Collaboration procedure
- What is RequestDispatcher?
- What are SendRedirect method and its use?
- About Servlet Config and Servlet Context
- About Session tracking and its techniques
- Cookies, Hidden Form Fields, URLs, etc
- About Events
- What is a Filter and what types of filters?
Let’s start with detailed answers.
Q #1) What do you mean by Servlet?
Answer: Servlet is a powerful mechanism that is used to create web applications.
It is occupied at the server side and helps to generate dynamic web pages. It acts as a mediator between the incoming HTTP request from the browser and the database.
Servlet is based on Java Language, hence, it is robust and called a server-side programming language.

The above diagram explains the flow of Servlet.
A request comes from the web page to the servlet, servlet redirects the request to the appropriate JSP page and the JSP page sends the response as a result page, which is visible to the user.
Recommended Reading =>> JSP Interview Questions
Q #2) What is CGI and what are its drawbacks?
Answer: CGI stands for Common Gateway Interface, which is a set of codes written on the server side that is used to interact through the Web Server with a client running on a web server.
It takes the incoming request and for every new request; it starts a new process.
Drawbacks of the Common Gateway Interface:
- As it creates a new process for every incoming request, if the number of incoming requests is more than the response generated will be very slow, which in turn reduces the efficiency.
- CGI is platform-dependent.
Q #3) What are the advantages of Servlet over CGI?
Answer: The advantages of the servlet are as follows:
- Servlet creates a thread for each incoming request and does not process, thus it is faster.
- Servlet is platform-independent as it is based on the Java Programming Language.
- As it is based on Java, it is also robust and secure.
Q #4) How is a Servlet implemented in code?
Answer: Servlet can be implemented in code by simply extending the Httpservlet or generic servlet class.
Q #5) What is the difference between the HTTP Servlet and Generic Servlet?
Answer: Generic Servlet can handle all types of requests. As it has a service () method, it is independent, whereas Http Servlet extends the generic servlet and supports the HTTP methods such as doGet (), doPost (), doHead (), doTrace (), etc.
Q #6) What are the life cycle methods of the Servlet?
Answer: There are three lifecycle methods of a servlet.
These are:
- Init ()
- Service ()
- Destroy ()
Q #7) Explain the Lifecycle of Servlet.
Answer: The life cycle of a servlet is explained in the diagram below .
- At first, the Servlet class is loaded as per the request received from the Client.
- Then the new instance or object of a servlet is created. Only one object is created for every life cycle.
- Then the Init () method, used to initialize the servlet, is invoked.
Syntax: public void Init ()
- The Service () method is invoked whenever a new request is received to perform any operations.
Syntax: public void service ()
- Then the destroy method is invoked to perform the clean-up operation.
Syntax: destroy ()
Servlet Flow Diagram

Q #8) What is a web container, and what is its responsibility?
Answer: A web container is also called Servlet container and is used to interact with the Servlet and contains all the Servlet, JSP, XML files in it. Web container manages the life cycle of a servlet and helps to map the URL to a specific servlet. Web container creates the object of a servlet.
Q #9) How is the Get () method different from the Post() method?
Answer: The reasons why the Get () method is preferred over the Post() method are given below.
Get () method:
- Here, a specific amount of data or information can be sent as the data is sent through the header.
- In the Get() method, data is not secured as it is exposed in the URL bar to the user.
- The Get () method can be bookmarked.
- Generally, the get () method is more effective and used over the post () method.
Post () method:
- Here, a huge amount of data or information can be transferred as the data is sent through the body.
- As the data in the Post () method is sent through the body, it is secured.
- The Post () method cannot be bookmarked.
- Generally, the Post () method is less effective and is not often used.
Q #10) What is Servlet looping or chaining?
Answer: Servlet looping is a process in which the output of one servlet is given as an input to another servlet and the last servlet output is considered as the actual output which is provided to the client.
This process is achieved through the request dispatcher interface.
Q #11) How will two or three servlets interact or communicate with each other?
Answer: There are two processes in which the servlets can communicate with each other.
- Request Dispatcher Interface
- Servlet Chaining
Q #12) Explain Request Dispatcher and its methods.
Answer: Request Dispatcher creates an object that is responsible for receiving requests from the browser or client and then navigates them to any resources like Servlets, JSP, or HTML that reside on the server side.
There are two methods of Request Dispatcher:
a) Forward () method:
- In the Forward() method, the client sends the request to Servlet1.
- The Servlet1 processes the request and then forwards the request to Servlet2.
- The servlet2 processes the request and generates a response which in turn is sent back to the client as the final response.
b) Include () method:
- In Include () method, the client sends the request to Servlet1.
- The Servlet1 processes the request and then includes the request and sends the request to Servlet2.
- The Servlet2 processes the request and again sends it back to Servlet1.
- The Servlet1 generates a response, which in turn is sent back to the client as the final response.
Q #13) What is the use of the Send Redirect () method?
Answer: Send Redirect () method which works on the client side is used to redirect the response to another resource like Servlet, JSP, HTML.
Syntax: void send Redirect(URL);
Example: response.sendredirect(“http://www.google.com”);
Q #14) How Forward () method is different from Send Redirect () method?
Answer:
Forward () method:
- It is used to send the same request to another resource.
- It works on the server-side within the server.
Send Redirect () method:
- It always sends a new request to the resources as it uses the URL.
- It works at the client side, both outside and within the server.
Q #15) Explain the WAR file.
Answer: A WAR file is basically referred to as a Web Archived file, which has all the files of your application like XML, servlets, JSP, HTML, and configuration files, combined into a single file so that deploying the application would be simple and easy.
It is advisable to use a WAR file for deployment.
Q #16) What do you mean by Servlet Context?
Answer: Servlet Context is referred to as an object that has information regarding the application and the Web Container. With the Servlet context, we can log events, get the URL of the specific resource, and can easily store the attributes for other servlets to use.
The core advantage of Servlet is that it is easy to maintain and acts as a mediator between the container and the servlet.
There are some important methods of the servlet context which are given below:
- getInitParameter () – return the value of the parameter.
- getInitParameterNames () – returns the name of the parameter.
- void setAttribute () – used to set the values of attributes.
- void getAttribute () – used to get the values of attributes.
- void removeAttribute () – used to remove the attribute.
Q #17) What exactly are the functions of Servlet?
Answer: The functions performed by the servlets are:
- Firstly, Servlets receive the HTTP request which is sent from the client-side.
- Reads the request and extract the data from the request.
- After extracting the information, the servlets perform a business logic operation by accessing a database or invoking EJBs.
- Lastly, it generates a response and sends it to the client as HTTP or sends the response to the JSP page.
Q #18) What do you mean by deployment descriptor?
Answer: WEB.XML is said to be the deployment descriptor for a servlet.
It is the entry point for any application and possesses the welcome file list. It defines resources, and information about which servlet will be used and maps the servlet to URL.
Q #19) Explain Session tracking and its importance.
Answer: Session tracking is a process in which the data of the client or user can be maintained.
As every time a new request comes to the server, the server cannot recognize that the new request is coming from the same client, to avoid this problem, a session tracking technique is used.
Session Tracking plays a vital role in recognizing the client or the request.
Q #20) What are the different Session Tracking Techniques?
Answer:
There are four types of techniques, which are given below:
a) Cookies: Cookies are small information that are added to multiple client requests.
Example: One request comes to the server; the server adds some cookies with the response. Now, when the same client sends the request to the server again, the server recognizes the user.
b) Hidden Form Field: Here, we use a hidden text field for maintaining the state of the user.
c) URL Rewriting: Here, we give an extra link for the next servlet to be mapped.
d) Http Session: Here, a specific ID is generated for each user, so the server can recognize the user.
Q #21) What are the Servlet events?
Answer: Events are nothing but occurrences. Even changing the condition of the object is also an event.
The event classes and interfaces are as follows:
- Classes: ServletRequestEvent, ServletContextEvent, HttpSessionEvent etc.
- Interfaces: ServletRequestListner, ServletContextListner, HttpSessionListner etc.
Q #22) What do you mean by a filter, and how does it work?
Answer: A Filter is basically used to filter out things.
In a similar manner Filter in servlet is an object that is introduced at the pre-processing of request and post-processing of request. Its major functions include conversion, encrypting and decrypting values, input validation on data capturing the IP address, and saving all incoming requests.
A filter is defined in web.xml and it can be removed from the web.xml so that there is no need to change the servlet resulting in cost reduction.
Diagram of Servlet Filter working

Q #23) Explain the load on start-up and its importance.
Answer: Load on start-up is an element defined in web.xml (deployment descriptor) which helps the servlet to load at the time of deployment, while the server is restarting.
The reason to use load on start-up is that the servlet is loaded on the first request received so initially it takes more time to load resulting in decreased efficiency if we define loan on start-up it loads the servlet while the server restarting which increases efficiency.
Load on start-up is also working on two values:
- Positive (0,1,2,3….): The lowest positive value will be loaded first.
- Negative: The servlet will be loaded when the first request is received.
Q #24) Is the servlet synchronized?
Answer: No, the servlets are not synchronized. If we want to make the servlet synchronized, we must implement SingleThreadInterface.
Q #25) What do you mean by Scope Object and what are its types?
Answer: Scope objects help to share information among web components via setattribute() and getattribute().
Types of Scope Objects are:
- Web Context
- Session
- Request
- Page
Q #26) What does the term Localization refer to?
Answer: Localization refers to the local tradition or language followed by the user. So, we add resources or elements to the particular website, like adding the Hindi language, so every user can understand.
Q #27) If the servlet receives multiple requests, how many objects will it create?
Answer: Servlet will create only one instance, no matter how many incoming requests it receives.
Q #28) What is the major difference between Servlet and Applet?
Answer: The major difference between Servlet and Applet is that the Servlet resides on the server side whereas the Applet resides on the client side in the web browser.
Q #29) Is it possible to have a Constructor inside the Servlet?
Answer: Yes, it is possible to define a constructor inside a servlet, but it can be called only by the Servlet container and not explicitly.
Q #30) Name the packages that work with Servlet.
Answer: Two packages work with Servlet as shown below:
- Javax.servlet
- Javax.servlet.http
Q #31) What are the kinds of HTTP requests?
Answer: Kinds of HTTP requests include:
- Get
- Post
- Head
- Options
- Put
- Trace
- Delete
Q #32) What is the major difference between Context Parameter and Context Attribute?
Answer: The major difference between the two is,
Context Parameter is a value stored in the deployment descriptor i.e. web.xml and is loaded during the deployment process. Whereas, Context Attribute is the values that are set dynamically and can be used throughout the application.
Q #33) What is the process for chaining servlets?
Answer: Servlet chaining is a very simple process in which we give the output of one servlet as input to another servlet.
Firstly, we have to create a RequestDispatcher for a resource that has to be chained. Then, we have to set the attribute values for the request if required. Then we need to call the forward () method or include () method on a RequestDispatcher object.
I’m sure that you would have got a clear picture of what Servlet is all about!
We wish you all success!!






