Servlet Interview Questions & Answers for Freshers and Experienced
Servlet interview questions and answers covering servlet lifecycle, request and response, sessions, filters, listeners, and real-world use cases.
Top Servlet Interview Questions for Freshers and Experienced Developers
Get interview-ready with Servlet interview questions that explain servlet lifecycle, HTTP request and response handling, session management, filters, listeners, and integration with web applications.
36 Questions2 PagesEasy · Medium · HardPage 1 of 2
1
What is a servlet?
Answer
A servlet is simply a class which responds to a particular type of network request - most commonly an HTTP request. Basically servlets are usually used to implement web applications. All servlets must implement the javax.servlet.Servlet interface, which defines servlet lifecycle methods.
Did you know it?
2
Explain functioning of web server with an example in context to servlet?
Answer
Suppose user made the request for the URL www.google.com/intl/en/privacy.html then: 1. web browser establishes a connection with www.google.com server by locating its IP address from a DNS server. 2. The web browser then sends a request to the server to retrieve the intl/en/privcy.html file. 3. The server respond with the info about the the requested HTML page and the contents of privacy.html file.
This process of sending a request and receiving a response from the server is known as transaction.An HTTP session remains open till a single transaction is completed.
Did you know it?
3
How performance of Servlet is better than CGI?
Answer
Performance of servlets is better than CGI scripts because CGI scripts need to be loaded in various process for every request. On the other hand, a servlet , once loaded in the memory, can be run multiple times on a single lightweight thread.
Did you know it?
4
What is the role of web server in a J2EE application?
Answer
1. Web server converts the request into an HTTPServletRequest object. 2. The HTTPServletRequest object is sent to a web compnent to establish a communication with javabeans components. 3. The HTTPServletResponse object is generated by the web component and finally the HTTPServletResponse object displys the data to the client as the resonse generated by the web server.
Did you know it?
5
What are the functions of Servlet container?
Answer
The main functions of Servlet container are: 1. Lifecycle management : Managing the lifecycle events of a servlet lik class loading, instantiation, initialization, service, and making servlet instances eligible for garbage collection. 2. Communication support : Handling the communication between servlet and Web server. 3. Multithreading support : Automatically creating a new thread for every servlet request and finishing it when the Servlet service() method is over. 4. Declarative security : Managing the security inside the XML deployment descriptor file. 5. JSP support : Converting JSPs to servlets and maintaining them.
Did you know it?
6
Explain Servlet Workflow.
Answer
When a client request is received by a web container , firstly the container maps the request with an appropriate servlet according to deployment descriptor. After the servlet is idenified, the web container loads the servlet class and creates an instance of the servlet. The servlet is then initialized by calling init() method. After calling the init() method, the service() ethod passess the request and response object to the web conatainer. The web container invokes the destroy() method to remove the servlet, if the servlet is not required.
Did you know it?
7
Can we define costructor in Servlet?
Answer
Yes, Servlet implementation classes can have constructor but there is no point in creating a constructor in servlet because Servlet require ServletConfig object for initialization which is created by container itself.
Did you know it?
8
What is difference between GenericServlet and HttpServlet?
Answer
javax.servlet.Servlet is interface, it defines methods for all the implementations - that's what interfaces usually do. javax.servlet.GenericServlet is protocol independent. It is abstract, so it is not to be directly instantiated. It is usable class to extend if you some day have to write servlet for protocol other than HTTP. javax.servlet.http.HttpServlet is abstract class to be extended if you want to communicate over HTTP protocol. Most likely you only have to care about this one.
Did you know it?
9
What is a deployment descriptor in Servlet?
Answer
Deployment descriptor is a configuration file for the web application and it’s name is web.xml and it resides in WEB-INF directory. Servlet container use this file to configure web application servlets, servlet config params, context init params, filters, listeners, welcome pages and error handlers.
With servlet 3.0 annotations, we can remove a lot of clutter from web.xml by configuring servlets, filters and listeners using annotations.
Did you know it?
10
What is the inter-servlet communication?
Answer
Based on the bussiness, we can have multiple servlets for specific business scenario. In that case it is required to have inter-servlet communication. We can achieve this in Servlet by using RequestDispatcher forward() and include() methods and provide additional attributes in request for other servlet use.
Did you know it?
11
Are Servlets Thread Safe? How to achieve thread safety in servlets?
Answer
1. The first aim for a servlet is to achieve thread safety by virtue of no shared state. Any shared state will fail to be that when the servlet is deployed into a load-balancing cluster. 2. Any local variables in service methods are thread safe because they are specific to each thread but for shared resource we can use synchronization to achieve thread safety in servlets.
Did you know it?
12
What is difference between ServletResponse sendRedirect() and RequestDispatcher forward() method?
Answer
1. RequestDispatcher forward() is used to forward the same request to another resource whereas ServletResponse sendRedirect() is a two step process. In sendRedirect(), web application returns the response to client with status code 302 (redirect) with URL to send the request. The request sent is a completely new request. 2. forward() is handled internally by the container whereas sendRedirect() is handled by browser. 3. We should use forward() when accessing resources in the same application because it’s faster than sendRedirect() method that required an extra network call. 4. In forward() browser is unaware of the actual processing resource and the URL in address bar remains same whereas in sendRedirect() URL in address bar change to the forwarded resource. 5. forward() can’t be used to invoke a servlet in another context, we can only use sendRedirect() in this case.
Did you know it?
13
Why HttpServlet class is declared abstract?
Answer
To have any useful behaviour, it is expected that you will have to override the methods. HttpServlet does not have useful functionality on its own.Making its constructors private would limit the ability for subclasses to be created.
The design of HttpServlet was probably not ideal -- as on many pages, forms especially, GET and POST logic should proceed at least partly along a common path. The design idea of HttpServlet however was to offer doGet(), doPost() etc implementations answering a 'not supported' error depending on HTTP version. These stubs would be useful to inherit if you needed to return such an answer.
In summary, the API/ interface is complete -- but the functionality is definitively not. Thus it is declared as abstract.
Did you know it?
14
What are life cycle methods of a servlet?
Answer
The javax.servlet.Servlet interface defines the life cycle methods of servlet such as init(), service() and destroy(). The Web container invokes the init(), service() and destroy() methods of a servlet during its life cycle.
Did you know it?
15
What are the different HTTP methods?
Answer
1. GET
2. POST
3. PUT
4. DELETE
5. OPTIONS
6. HEAD
7. TRACE
Did you know it?
16
Which HTTP method is non-idempotent?
Answer
A HTTP method is said to be idempotent if it returns the same result every time. HTTP methods GET, PUT, DELETE, HEAD, and OPTIONS are idempotent method and we should implement our application to make sure these methods always return same result. HTTP method POST is non-idempotent method and we should use post method when implementing something that changes with every request.
Did you know it?
17
What is MIME Type?
Answer
The Content-Type response header is known as MIME Type. Server sends MIME type to client to let them know the kind of data it’s sending. It helps client in rendering the data for user. Some of the mostly used mime types are text/html, text/xml, application/xml etc.
Did you know it?
18
What is a web application and what is it’s directory structure in Servlet?
Answer
Web Applications are modules that requires to run on server to handle the request and return the response. Java provides web application support through Servlets and JSPs that can run in a servlet container and provide dynamic content to client browser.
Java Web Applications are packaged as Web Archive (WAR) and it has following folders in it. WEB-INF : contains the lib directory(application dependencies), classes(contains java servlet files) and web.xml(Deployment Descriptor) META-INF: contains MANIFEST.MF webapp: contains all the static pages like .html , .js etc
Did you know it?
19
What is Servlet Chaining?
Answer
It is a method in which the output of one servlet is piped into the next servlet.
It is the last servlet in the chain that provides the output to the Web browser.
Did you know it?
20
What is ServletConfig object?
Answer
javax.servlet.ServletConfig is used to pass configuration information to Servlet. Every servlet has it’s own ServletConfig object and servlet container is responsible for instantiating this object by calling servlet's init() method.A servlet is initialized only once in its life cycle and hence the init() method is called only once per servlet by a web server.