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.

Q11. What is the difference between forward () and sendRedirect () functions in Servlet? Explain
  forward() sendRedirect()
1. Server-side redirect. Client-side redirect.
2. Redirect is at the server end, not visible to the client. Redirection is at the client end and visible to the client.
3. Original URL remains intact. Original URL changes.
Q12. Explain the jar and war files in servlet?

Both WAR and JAR have the same file structure of a single file that is compressed and contains multiple files inside it. WAR files combine servlets, JSPs, Java and XML files, JAR libraries, javascript libraries, static web pages, and other resources that are needed to run the applications.

Q13. What are the types of an HTTP request?

HTTP outlines a set of request methods that indicate the action to be performed for a resource. Each request method implements a different semantic, but they also share some common features. Here are the 7 HTTP request methods every web developer preparing for Servlet interview questions and answers should know:

  • GET
  • HEAD
  • POST
  • PUT
Q14. How we can create war file in servlet?

In order to create a WAR file, use JDK's JAR tool. Use -c switch of jar for creating the war file. Enter the project directory of your project and write this command: jar -cvf projectname.war * Here, -c is for creating file, -v for generating verbose output and -f for specifying name of the archive file.

Q15. What do you mean by SingleThreadModel interface?

The SingleThreadModel interface ensures that only one thread is executed at a given point of time in a given servlet service method. This is a marker interface and contains no methods. This interface also ensures thread-safe servlets.

Q16. What is URL Encoding in Servlet?

URL Encoding process transforms user input to a Common Gateway Interface (CGI) form. It is fit to travel across the network through stripping spaces, punctuation and replacement with escape characters.

Q17. How we can get IP address of client in servlet?

Here are 2 methods for getting client’s IP address in Servlet:

HttpServletRequest.getRemoteAddr method returns IP of the client that sends request.

HttpServletRequestgetRemoteHost method returns the hostname of the client that is sending the request. However, it may return an empty string if hostname is unknown.

Q18. Explain the features are in Servlet 3?

Servlet 3.0 is an update of Servlet 2.5. Servlet 3.0 focuses on extensibility and pluggability. Key features in Servlet 3.0 can be categorized into the following areas:

  • Easier development with annotations.
  • Web application modularity.
  • Asynchronous Servlet.
  • Programmatic instantiation of Servlets/Filters.
  • Security Enhancements.
Q19. Explain the different ways for servlet authentication?

There are ways of servlet authentication:

  • HTTP basic authentication.
  • HTTP digest authentication.
  • HTTPS client authentication.
  • Form-based authentication.
Q20. How to call a jsp from the servlet?

You can invoke a JSP web page from a servlet via the functionality of the popular javax.servlet.RequestDispatcher interface. Complete the following steps in your code to use this mechanism:

  • Get a servlet context instance from the servlet instance:
    ServletContext sc = this.getServletContext();
  • Get a request dispatcher from the servlet context instance, specifying the page-relative or application-relative course of the goal JSP page as enter to the getRequestDispatcher() method:
    RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp");
    Prior to or in the course of this step, you can optionally make facts on hand to the JSP page thru attributes of the HTTP request object. See the next section, "Passing Data Between a JSP Page and a Servlet", for information.
  • Invoke the include() or forward() approach of the request dispatcher, specifying the HTTP request and response objects as arguments. The performance of these techniques is similar to that of jsp:include and jsp:forward actions. The include() approach solely briefly transfers control; execution returns to the invoking servlet afterward.

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...