Servlet Interview Questions

Servlet interview questions

Choosing a career and sitting in the interview rooms is difficult for an individual to analyze and choose if you don’t work it out smartly. But if you are worried thinking about what will be your career option, then Servlet is the best field that you can choose to work. With the help of our Servlet interview questions, you can determine what kind of questions are generally asked in the interview and how to answer them strategically. All these questions are prepared by the experts and professionals who have sufficient knowledge and expertise in this field. Therefore, you can rely on us when it comes to finding the right questions and answers.

About Servlet

Servlets are basically java programs that operate in the web servers or the application servers using Java programs. They are utilized in the servers in such a way that they can handle the requests that are obtained from the servers, analyze it, process it, create a response, and send them back to the servers from where it originated. Servlets work on the sides of services as it possesses the properties to work on the side of servers. It most commonly implies web containers for hosting various web applications on the services and maintain the quality of the APIs.t

Best Servlet Interview Questions of 2020

1. What is Servlet?

Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

2. What are the advantages of servlets over CGI?

Servlets offer several advantages in comparison with the CGI.

  • Performance is significantly better.

  • Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.

  • Servlets are platform-independent because they are written in Java.

  • Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.

  • The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.

3. How many objects of a servlet is created?

Only one object at the time of first request by servlet or web container.

4. What is the life-cycle of a servlet?

  1. Servlet is loaded
  2. servlet is instantiated
  3. servlet is initialized
  4. service the request
  5. servlet is destroyed

5. What are the life-cycle methods for a servlet?

Method Description
public void init(ServletConfig config) It is invoked only once when first request comes for the servlet. It is used to initialize the servlet.
public void service(ServletRequest request,ServletResponse)throws ServletException,IOException It is invoked at each request.The service() method is used to service the request.
public void destroy() It is invoked only once when servlet is unloaded.

6. Who is responsible to create the object of servlet?

The web container or servlet container.

7. When servlet object is created?

At the time of first request.

8. What is difference between Get and Post method?

Get Post
1) Limited amount of data can be sent because data is sent in header. Large amount of data can be sent because data is sent in body.
2) Not Secured because data is exposed in URL bar. Secured because data is not exposed in URL bar.
3) Can be bookmarked Cannot be bookmarked
4) Idempotent Non-Idempotent
5) It is more efficient and used than Post It is less efficient and used

9. What is difference between PrintWriter and ServletOutputStream?

PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information whereas ServletOutputStream class can be used to write primitive values as well as character-based information.

11. What is difference between GenericServlet and HttpServlet?

The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific. HttpServlet provides additional functionalities such as state management etc.

12. What is servlet collaboration?

When one servlet communicates to another servlet, it is known as servlet collaboration. There are many ways of servlet collaboration:

  • RequestDispacher interface
  • sendRedirect() method etc.

13. What is the purpose of RequestDispatcher Interface?   

The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp. This interceptor can also be used to include the content of antoher resource.

14. Can you call a jsp from the servlet?

Yes, one of the way is RequestDispatcher interface for example:

  1. RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");  
  2. rd.forward(request,response);  

15. Difference between forward() method and sendRedirect() method?

forward() method sendRedirect() method
1) forward() sends the same request to another resource. 1) sendRedirect() method sends new request always because it uses the URL bar of the browser.
2) forward() method works at server side. 2) sendRedirect() method works at client side.
3) forward() method works within the server only. 3) sendRedirect() method works within and outside the server.

16. What is difference between ServletConfig and ServletContext?

The container creates object of ServletConfig for each servlet whereas object of ServletContext is created for each web application.

17. What is Session Tracking?

Session simply means a particular interval of time.

Session Tracking is a way to maintain state of an user.Http protocol is a stateless protocol.Each time user requests to the server, server treats the request as the new request.So we need to maintain the state of an user to recognize to particular user.

18. What are Cookies?

A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

19. What is difference between Cookies and HttpSession?

Cookie works at client side whereas HttpSession works at server side.

20. What is filter?

A filter is an object that is invoked either at the preprocessing or postprocessing of a request. It is pluggable.

21. How can we perform any action at the time of deploying the project?

By the help of ServletContextListener interface.

22. What is the disadvantage of cookies?

It will not work if cookie is disabled from the browser.

23. How can we upload the file to the server using servlet?

One of the way is by MultipartRequest class provided by third party.

24. What is load-on-startup in servlet?

The load-on-startup element of servlet in web.xml is used to load the servlet at the time of deploying the project or server start. So it saves time for the response of first request.

25. What if we pass negative value in load-on-startup?

It will not affect the container, now servlet will be loaded at first request.

26. What is war file?

A war (web archive) file specifies the web elements. A servlet or jsp project can be converted into a war file. Moving one servlet project from one place to another will be fast as it is combined into a single file.

27. How to create war file?

The war file can be created using jar tool found in jdk/bin directory. If you are using Eclipse or Netbeans IDE, you can export your project as a war file.

To create war file from console, you can write following code.

  1. jar -cvf abc.war *  

Now all the files of current directory will be converted into abc.war file.

28. What are the annotations used in Servlet 3?

There are mainly 3 annotations used for the servlet.

  1. @WebServlet : for servlet class.
  2. @WebListener : for listener class.
  3. @WebFilter : for filter class.

29. Which event is fired at the time of project deployment and undeployment?


30. Which event is fired at the time of session creation and destroy?


31. Which event is fired at the time of setting, getting or removing attribute from application scope?


32. What is the use of welcome-file-list?

It is used to specify the welcome file for the project.

33. What is the use of attribute in servlets?

Attribute is a map object that can be used to set, get or remove in request, session or application scope. It is mainly used to share information between one servlet to another.

34. What are the uses of servlets?

Servlets are used to process and store data submitted by HTML form, dynamic content, handle multiple request concurrently and manage state information on top of stateless HTTP.

35. Whether we can get deadlock situation in Servlets?

Yes, it can be achieved by writing doGet method in doPost method and writing doPost method in doGet method.

Career scopes and salary scale

As most of the companies run their databases and programs on Java, there is a huge scope and opportunity for you to pursue a career in this specific field. The knowledge and expertise that you will earn within a few years will definitely allow you to start your own company and run it effectively. If you are thinking to pursue your career in this field, then you need to have the right skills and talent in order to huge lump sum amount of money per month. However, initially, for a fresher, he or she can earn up to 55,000 to 65,000 dollars per annum, while the experienced candidates can earn up to 97,000 to 100,000 dollars per annum.


Now, it’s easy to crack the interviews that you are attending to get a job by following our Servlet interview questions and answers. If you have any queries, you can just drop us a message, and we will ensure that an expert looks into it immediately and clears your doubt.

Intellinuts Terms : A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z