Servlet Interview questions and Answers

Last updated on Mar 11, 2022
  • Share
Servlet Interview questions

The Servlet is a class used in Java applications for enhancing the capabilities of the server. Java servlets run on web application server container and are used to generate HTML content that shows on the web browser. Servlets respond to request from the browser and return response content.  Our new collection of Servlet interview questions is a must-read for all developers. Servlets are mostly implemented for HTTP protocol and are also called as HTTP Servlet.

About Servlet
What is Servlet Servlets generate dynamic HTML content and respond with excel, XML, pdf, and JSON and other formats. Initially, servlets were meant for creating HTML content in applications, but now they are mostly used at the controller layers.
Latest Version 4.0, released in September 2017.
Created By Pavni Diwanji
Specification JSR 369
Platform Java EE 8
Important Changes HTTP/2

Most Frequently Asked Servlet Interview questions

Here in this article, we will be listing frequently asked Servlet Interview questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q1. What are the difference between Servlet and CGI?
Answer
  Servlet CGI
1. Portable Not portable
2. Requests handled by lightweight Java Thread Requests handled by heavyweight OS process
3. Data sharing possible Data sharing not available
4. Link directly to the Web server Cannot link directly to the Web server
Q2. Explain the lifecycle of a Servlet?
Answer

Servlert’s life cycle is the entire process from its creation until its destruction. A Servlet follows this path:

  • Initialized by calling init( ) method
  • Calls service( ) method to process request
  • Terminated by destroy( ) method

Finally, the servlet is nothing but the garbage that gets collected by JVM's garbage collector.

Q3. What is Servlet and list its types?
Answer

The Servlet is a class used in Java applications for enhancing the capabilities of the server. Servlets respond to a request from the browser and return response content and also run on web application server container and are used to generate HTML content that shows on the web browser.

There are two types of servlets - generic and HTTP

Q4. Explain the difference between generic Servlet and HTTP Servlet?
Answer
  Generic Servlet HTTP Servlet
1. Subclass of javax.servlet.GenericServlet Subclass of javax.servlet.HttpServlet
2. Protocol independent Protocol dependent
3. Belongs to javax.servlet package Belongs to javax.servlet.http package
Q5. Explain the difference between GET and POST method in Servlet?
Answer
  GET method POST method
1. A limited amount of data can be sent as data is sent in the header Lot of data can be sent as data is sent in the body
2. Not secured because data is exposed Secured because data is not exposed
3. Can be bookmarked Cannot be bookmarked
Q6. Explain the difference between ServletConfig and ServletContext in Servlet?
Answer
  ServletConfig ServletContext
1. Parameters specified for a specific servlet. Parameters specified for the entire application.
2. The object created during the initialization process. The object created during web application deployment.
3. Available as long as the servlet is executing. Available as long as the application is executing.
Q7. What do you mean by Session Tracking and also explain its techniques?
Answer

Session tracking is a technique that servlets use to maintain a record of a series of requests originating from the same user or the same browser across a period of time. These sessions are shared between the servlets that are accessed by a client.

There are 4 techniques to track sessions:

  • Cookies
  • Hidden Fields.
  • URL Rewriting.
  • Session Tracking API.
Q8. What are the difference between session and cookies in Servlet? Explain
Answer
  Sessions Cookies
1. Server-side files. Client-side files.
2. Stores unlimited content in user's browser. Stores limited content of limited users.
3. Data is not easy to modify. Data is easy to modify.
Q9. What do you mean by filter in servlet?
Answer

A filter is an object that filters tasks that are either request to a resource, or response from a resource, or both. Filtering is performed in doFilter method. Filters get configured in deployment descriptor of your application.

Q10. What do you mean by Request Dispatcher in Servlet? Also explain its methods.
Answer

RequestDispatcher interface defines an object that can dispatch the request to resources like HTML, JSP, and Servlet, on the server. This interface provides two methods

forward (ServletRequest request, ServletResponse response) will forward a request from Servlet to another resource such as a Servlet, JSP, or HTML, on the server.

Include (ServletRequest request, ServletResponse response) will include the content of a resource such as Servlet, JSP, or HTML, in the response.

An Quick Overview of Servlet

In Java, servlets generate dynamic HTML content and respond with excel, XML, pdf, and JSON and other formats. Initially, servlets were meant for creating HTML content in applications, but now they are mostly used at the controller layers.

Our jsp servlet interview questions for experienced professionals is a huge hit amongst potential candidates looking for new jobs. Have you read them yet?

Development History of Servlet

Servlet 1.0 was released on June 1997.

Latest Version

Latest Version: Servlet 4.0 is the newest version that was released on September 2017 on the Java EE 8 platform. You should read in-depth about different versions and their features if you are preparing for tricky jsp and servlet interview questions .

Reviewed and verified by Best Interview Question
Best Interview Question

With our 10+ experience in PHP, MySQL, React, Python & more our technical consulting firm has received the privilege of working with top projects, 100 and still counting. Our team of 25+ is skilled in...