Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Monday, July 26, 2021

Top 10 Servlet Interview Question Answers for Java JEE Developers

This time its servlet interview questions, I was thinking about what to pick for my interview series and then I thought about J2EE and Servlet is my favorite in that space. Servlet is an important part of any J2EE development and serves as a Controller on many web MVC frameworks and that’s why it’s quite popular on J2EE interviews. These Servlet questions are based on my experience as well as collected by friends and colleague and they are not only good for interview practice but also shows a new direction of learning for anyone who is not very familiar with servlet technology.

As said earlier this interview question article is part of my earlier series of java interview questions, UNIX command interview questions, and Java threading interview questions.

You can find answers to all these questions on google but I have also listed my answers for quick reference.

Servlet Questions Asked in Interview

Question 1: In web.xml file   <load-on-startup>1</load-on-startup> is defined between <servlet></servlet> tag what does it means. (detailed answer)
Ans: Whenever we request for any servlet the servlet container will initialize the servlet and load it which is defined in our config file called web.xml by default it will not initialize when our context is loaded.

Defining like this <load-on-startup>1</load-on-startup> is also known as pre-initialization of servlet means now the servlet for which we have defined this tag has been initialized in starting when the context is loaded before getting any request. 

When this servlet question was asked to me in an interview a few years back, I was not even aware of this element but this question pointed me to look DTD of web.xml and understand other elements as well.

Question 2: How can we create deadlock condition on our servlet? (detailed answer)
Ans: one simple way to call doPost() method inside doGet() and doGet()method inside doPost() it will create deadlock situation for a servlet. This is rather a simple servlet interview questions but yet tricky if you don’t think of it J

Question 3: Explain Servlet Life Cycle in Java EE environment?
A picture is worth a thousand words, here is a diagram that explains the Servlet life cycle:

Servlet LifeCycle Interview Question

Question 4: Why super.init (config) is the first statement inside init(config) method(detailed answer)
Ans: This will be the first statement if we are overriding the init(config) method by this way we will store the config object for future reference and we can use by getServletConfig()  to get information about the config object if will not do this config object will be lost and we have only one way to get config object because servlet passes config object only in init method . Without doing this if we call the ServletConfig method will get NullPointerException.

Question5: Can we call destroy() method inside the init() method is yes what will happen? (detailed answer)
Ans: Yes we can call like this but if we have not overridden this method container will call the default method and nothing will happen.after calling this if any we have overridden the method then the code written inside is executed. You can further see these Servlet and JSP courses to learn more. 

Question 6: How can we refresh the servlet on the client and server-side automatically? (detailed answer)
Ans: On the client-side, we can use Meta HTTP refresh and server-side we can use server push.

Question 7: How can you get the information about one servlet context in another servlet(detailed answer)
Ans: In context object we can set the attribute which we want on another servlet and we can get that attribute using their name on another servlet.

Context.setAttribute (“name”,” value”)
Context.getAttribute (“name”)

Question 8: Why do we need to implement a Single Thread model in the case of Servlet. (detailed answer)
Ans: In J2EE we can implement our servlet in two different ways either by using:
1. Single Thread Model
2. Multithread Model
Depending upon our scenario, if we have implemented single thread means only one instance is going handle one request at a time no two thread will concurrently execute service method of the servlet.

The example in banking accounts where sensitive data is handled mostly this scenario was used this interface is deprecated in Servlet API version 2.4.

As the name signifies multi-thread means a servlet is capable of handling multiple requests at the same time. This servlet interview question was quite popular a few years back on entry-level but now it's losing its shine.

SingleThread Servlet interview question

Question 9: What is servlet collaboration? (detailed answer)
Ans communication between two servlets is called servlet collaboration which is achieved by 3 ways.
1. RequestDispatchers include () and forward() method .
2. Using sendRedirect()method of Response object.
3. Using servlet Context methods

Question 10: What is the difference between ServletConfig and ServletContext? (detailed answer)
Ans: ServletConfig as the name implies provide the information about the configuration of a servlet which is defined inside the web.xml file or we can say deployment descriptor.its a specific object for each servlet.

ServletContext is an application specific object which is shared by all the servlet belongs to one application in one JVM .this is a single object which represents our application and all the servlet access application specific data using this object.servlet also use their method to communicate with the container.

Question 11: For initializing a servlet can we use a constructor in place of init()? (detailed answer)
Ans: No, we can not use the constructor for initializing a servlet because for initialization we need an object of servletConfig using this object we get all the parameter which are defined in deployment descriptor for initializing a servlet and in servlet class we have only default constructor according to older version of java so if we want to pass a Config object we don’t have parametrized constructor and apart from this servlet is loaded and initialized by container so it's a job of container to call the method according to servlet specification they have lifecycle method so init() method is called firstly.

More important Java doesn't allow interfaces to declare constructors. These kinds of servlet interview questions are quite popular on service based companies who just want to dig one level more. You can also refer to Programming Interviews Exposed for more of such questions. 

Servlet interview questions and answers

Question 12: What is the difference between HttpServlet and GenericServlet in Servlet API? (answer)
GenericServlet provides framework to create a Servlet for any protocol e.g. you can write Servlet to receive content from FTP, SMTP etc, while HttpServlet is built-in Servlet provided by Java for handling HTTP requests. See detailed answer for deep discussion.

These Servlet interview questions are good for the quick recap of important concept before appearing on any J2EE interview. Please share if you have come across any other interesting interview question on Servlets.


Jsguy said...

Excellent site. One q I was asked is how to prevent double submission from browser in which JavaScript is disabled?

Javin @ convert string to int in java said...

hi Jsguy, that's interesting question, Please share answer as well and how your interview went?


Leskivov said...

Good collection of Servlet Interview questions Answers but I am looking pdf version of Servlet interview questions in Java , Can you please let me know how can I download Servlet interview questions pdf from your blog. Thanks

Ricky said...

Another good servlet interview question is What is load-on-startup tag in web.xml do and what does value of zero for load on startup means ?

Anonymous said...

Does calling doGet() from doPost() and vice versa really cause a deadlock? Or, does it cause a StackOverflowException?

Deepak said...

how about these Servlet questions:

1. What is initialization order of servlet ? What are lifecycle method and who calls them?
2. How Servlet and JSP are related to each other ?
3. What is Servlet Container, What are responsibilities of Servlet Container?
4. What is difference between ServletContext and ServletConfig?
5. How to control initialization of Servlet? How will you ensure pre initialization of Servlet?
6. What is difference between GenericServlet and HttpServlet?
7. What is difference between doGET() and doPOST() method?
8. Who calls doGET() and doPOST() method ?
9. Who creates object of HttpServletRequest and HttpServletResponse?
10. How do you make a Servlet thread safe ?
11. What is difference between Servlet and Filter?
12. What is lifecycle of filter?
13. How do you make your application to run using SSL?
14. How do you ensure that a particular servlet can only be accessed after authetincation?
15. How to implement authentication and authorization in Servlet application?
16. What is advantage of Servlet over CGI?

Its not easy to answer all these Servlet question and only guys who has extensive experience in Servlet and JSP can answer with confidence :)

Pushkar said...

recently interviewers are started asking questions from Servlet 3.0, these are the few servlet interview questions which were based on Servlet 3.0 API:

1. What are major enhancement or features added in Servlet 3.0 API (async processing, annotation, modularization of web.xml etc.)

2. What is asynchronous processing in Servlet? Why do you need that?

3. How do you upload a file using Servlet 3.0

these are just few questions guys, but its worth reading Servlet 3.0 API at least for interviews.

Aarav said...

There are really good, tricky and advanced servlet question in comments. hope you can provide answers of these tricky question as well. Servlet is not a tough topic on interview but some questions especially from Filter like How to use ThreadLocal variable using Filter in Java is real tricky Servlet question to answer. Servlet Question provided by Pushkar is updated and new and more chances to appear in Senior level interview like 5+ years experience on J2EE. like What is asynchronous processing in Servlet etc.

Javin @ xml interview questions said...

Comment from Gaurav Dubey:
Hi Javin

If you can post some interview questions about Java Filters that would be good. Because filters are getting addressed in interviews.
When a programer should write a filter?
What is the main purpose of filter?
How it works ?

Interviewers ask these questions.

Hi @Gaurav Dubey, are you talking about Filters used from Servlet API ? Yes, I will try to do that. By the way if you look at framework like Spring Security, Display tag they use Filters to implement Security and overriding response.

Sorry I have moved your comment on this page but it seems more appropriate to discuss Servlet Filters.


Arjun said...

Calling doGet() from doPost() and vice versa won't create deadlock. It just become a infinite loop and cause the StackOverflowException. It might look like a dead lock, but that's not a deadlock in the technical sense of the word.

Unknown said...

Hi Javin, Can I use GET request instead of PUT to create resources??

Anonymous said...

How to stop a servlet manually/forcefully. If there are 10 servlet, how to stop only one servlet that has an error.

I was asked this question in interview

Anonymous said...

@Anonymous, you cannot stop a Servlet manually, at most you can undeploy the application. JMX provides runtime control but I am not sure if its avaiable with Servlet or not.

Anonymous said...

Can someone answer the questions listed in the comments section?

antara said...

The below link is for the question "How to stop servlet manually"
link :

Anonymous said...

Nice Article

Anonymous said...

very useful site

Anonymous said...

Click ctrl+p and save as pdf in chrome

Post a Comment