Register Login

Java Servlet Interview Questions and Answers

Updated Jul 18, 2019

What is a Java Servlet?

Servlets are Java programs that run on the web application server. They are used to accept the request received by the server, process the request and send a response back to the server. It is a web component that is executed on the server to develop dynamic web pages.

What are some important characteristics of Servlet?

Some important characteristics of Servlet are:

  • It is server-side technology and can be used for developing applications
  • It has an API that includes documentation, classes, and interfaces
  • As it is written in Java, the code is portable and can be executed in any operating system like Unix and Windows
  • It is scalable and is invoked using a thread by the server as respond to client requests
  • It is less prone to memory leaks and utilizes the facilities like exception handling and garbage collection of Java]

What are the types of Servlet in Java?

The different types of Servlet are as follows:

  • Generic servlet
  • Http servlet

What is a Servlet container?

The Servlet container provides Java applications in a runtime environment. On the client-side, only static web pages can be requested by the user. In case they want to read the content of the web page according to the input, the Servlet container comes into play. It is used to generate web pages dynamically on the server-side.

What is the hierarchy of the servlet ecosystem?

In the Servlet class, the Servlet interface is the root interface. All Servlets need to implement this interface. In the Servlet API, the Generic Servlet class implements the Servlet, ServletConfig interface and Serializable interface of java.io. package. When the Servlet is initialized, the ServletConfig interface is used to provide configuration data to the Servlet.

The Generic Servlet class is extended by the HttpServlet class to include HTTP communication facility into a Servlet.

What is the servlet context?

ServletContext is an interface whose object is used to develop a bridge between the servlet and the container. It has other uses:

  • Obtaining information from the web.xml file
  • For establishing communication between two applications
  • To set, remove or get an attribute from the web.xml file

Commonly used methods are public String getInitParameter(String name) and public Object getAttribute(String name).

What is dispatcher servlet?

DispatcherServlet is used in web applications based on the Spring framework. It acts as the front controller that receives the requests and sends them to other components for further processing. It is the single entry point for a request that is forwarded to MVC controllers. It uses Spring configuration classes for exception handling, viewing resolution request mapping.

How to call a servlet from JSP?

A servlet can be called from a JSP page, through the following code:

<jsp:include page="/servletURL" />

When the servlet is called or invoked, the JSP program is called to display certain results. If the URL pattern can match the <url-pattern> in the web.xml, the servlet can be called.

Difference between Post method and Get method in servlet?

In the GET method, the contents of a specific URL are requested by the browser. After the request is raised, the servlet’s doGet() method is called. In the POST method, the data is sent to the specific URL. Data entered by the user in a form may be sent this way. The servlet’s doPost() method is called.

How to pass value from one Servlet to another Servlet?

Values can be passed from one servlet to another using the following methods:

  • Calling servlets from another servlet by the requestDispatcher.forward() and requestDispatcher.include
  • Servlet chaining, where the output of one servlet can act as the input for another servlet
  • Two servlets can also communicate through the GET and POST methods. Here the QueryString can be used and the data can be later retrieved using the request.getParameter method

Differentiate between SendRedirect and RequestDispatcher in servlet?

SendRedirect RequestDispatcher
It is a method of the HttpServletResponse object It can be accessed from the HttpServletRequest interface
The method is used for redirecting the user’s request to another location for processing. The request is transferred using the browser It dispatches the request to another resource that can be a servlet, JSP page or HTML page.
The user can see the page where they will be redirected. The user cannot see where the request is directed as it is done internally.
The entire request processing is performed on the client-side The request is processed on the server-side

What is the use of listener in servlet?

In a web application, an event can be any occurrence such as a client request, creation or destruction of a session, initiating an application, etc. The servlet API provides many listener interfaces that can be implemented for configuring the web.xml file, to take action when an event occurs. For example, the ServletContextListner is used to listen to startup and shutdown events of any context.

What is MIME type in servlet?

MIME stands for Multipurpose Internet Mail Extensions, also known as Content-type is an HTTP header. It describes the content that is being sent to the browser. It is an internet standard that is used for enhancing the functionalities of the email so that images, sounds, and videos can be sent. The format is used for sending the response to the user.

MIME supports ASCII characters and unlimited length of messages.

How to run servlet program in Tomcat?

The steps to run a servlet in Apache Tomcat are as follows:

  • Create a directory for the application under web apps directory. Then create an src directory for the source code files and a WEB-INF directory for the Java classes that will be used
  • Write the code for the servlet. In the source code file, include the javax.servlet package and the javax.servlet.http package
  • Compile the servlet code
  • Create the deployment descriptor called web.xml
  • Run the Tomcat server
  • Invoke the servlet from the browser

What is a Generic servlet?

The GenericServlet class is protocol independent and is able to handle all types of requests. It can implement the Serializable, ServletConfig and Servlet interfaces.

Generic servlet overrides the service() method to accept the client request. It includes simplified versions of different lifecycle methods that make writing servlets easier.

What is servlet life cycle?

The different steps in the life cycle of a servlet are:

  • The Servlet class is loaded at first
  • An instance of the Servlet is created by the web container
  • The web container invokes the init method
  • The service method is called when a request for a servlet is raised
  • Before removing the instance from the service, the web container calls the destroy method. Memory or thread cleanup is performed before destroy() is called

Differentiate between HttpServlet and GenericServlet?

The differences between HttpServlet and Generic Servlet are:

HttpServlet Generic Servlet
Has to be used with only HTTP protocol It can be used with any protocol as it is protocol independent
The service() method does not have to be overridden The service() method has to be overridden as it is abstract
All methods are non-abstract The service() method is abstract and the other methods are concrete
Extends GenericServlet and implements Serializable It can implement the Serializable, ServletConfig and Servlet interfaces and extend Object
A direct subclass of GenericServlet A Direct subclass of Servlet interface

When and How the destroy method is called in Servlet?

The servlet web container calls the destroy() method to specify that the servlet is going to take it out of service. It is invoked when the threads in the service method have exited or a specific timeout period.

Which method of servlet interface to be used for handling request?

The service() method of the Servlet interface is used for handling requests.

What is init() method in Servlet?

The init() method calls the Servlet to initialize it before the first request is handled. This method is invoked only once.


×