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 2 of 2
1
Why super.init (config) is the first statement inside init(config) method?
Answer
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 config object if will not do this config object will be lost and we have only one way to get config object because servlet pass config object only in init method . Without doing this if we call the ServletConfig method will get NullPointerException.
Did you know it?
2
What is ServletContext object?
Answer
javax.servlet.ServletContext interface provides access to web application parameters to the servlet. The ServletContext is unique object and available to all the servlets in the web application. When we want some init parameters to be available to multiple or all of the servlets in the web application, we can use ServletContext object and define parameters in web.xml using element.
Did you know it?
3
What is difference between ServletConfig and ServletContext?
Answer
ServletConfig is a unique object per servlet whereas ServletContext is a unique object for complete application.
ServletConfig is used to provide init parameters to the servlet whereas ServletContext is used to provide application level init parameters that all other servlets can use.
We can’t set attributes in ServletConfig object whereas we can set attributes in ServletContext that other servlets can use in their implementation.
Did you know it?
4
Can we call destroy() method inside the init() method is yes what will happen?
Answer
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.
Did you know it?
5
What is the difference between include() and forward() in Servlet?
Answer
include() Includes the response of another servlet into the calling servlet. This method can be invoked from calling servlet while servicing the request. It includes contents of resource such as Servlet, JSP page or HTML page in response. forward() mehod is used to forward requests to resource such as Servlet, JSP file or HTML page on the server. I implies that after invoking forward() method, the servlet can not add any response content.
Did you know it?
6
What is Request Dispatcher?
Answer
RequestDispatcher interface is used to forward the request to another resource that can be HTML, JSP or another servlet in same application. We can also use this to include the content of another resource to the response. This interface is used for inter-servlet communication in the same context.
There are two methods defined in this interface: void forward(ServletRequest request, ServletResponse response) – forwards the request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. void include(ServletRequest request, ServletResponse response) – includes the content of a resource (servlet, JSP page, HTML file) in the response.
Did you know it?
7
What is difference between PrintWriter and ServletOutputStream?
Answer
1.PrintWriter is a character-stream class whereas ServletOutputStream is a byte-stream class. 2.We can use PrintWriter to write character based information such as character array and String to the response whereas we can use ServletOutputStream to write byte array data to the response. 3.We can use ServletResponse getWriter() to get the PrintWriter instance whereas we can use ServletResponse getOutputStream() method to get the ServletOutputStream object reference.
Did you know it?
8
What is session management and What are different methods of session management in servlets?
Answer
Http is a stateless, client-side pull only protocol.To implement a stateful conversation over it, Java EE Web Server need to hide some information (which is sessionid) in client-side and the mechanism it can use should follow HTTP and HTML spec.
There are three ways to accomplish this goal: Cookies - Server will ask browser to maintain a cookie. Hidden Form Field - server will add an additional parameter at every form in HTML URL Rewriting - Server will add an additional parameter at the end of URL link
Did you know it?
9
How do we manage session using cookies?
Answer
Using cookies is the simplest and easiest way to track a session.A unique session id (stored in the form of a cookie) is sent by the server to the client as a part of the response and the same session id saved with the client is sent to the server as a part of the request which helps the server to recognize the unique client session.
Did you know it?
10
What is the difference between encodeRedirectUrl and encodeURL?
Answer
encodeURL() is used for all URLs in a servlet's output. It helps session ids to be encoded with the URL. encodeRedirectURL() is used with res.sendRedirect only. It is also used for encoding session ids with URL but only while redirecting.
Did you know it?
11
Why servlet filter is required?
Answer
Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes:
To intercept requests from a client before they access a resource at back end.
To manipulate responses from server before they are sent back to the client.
Did you know it?
12
How to handle exceptions thrown by application with another servlet?
Answer
We can configure them in web.xml like below:
<error-page>
<error-code>400</error-code>
<location>/BadRequestExceptionHandler</location >
</error-page >
Usually servlet container loads a servlet on the first client request but sometimes when the servlet is heavy and takes time to loads, we might want to load it on application startup. We can use load-on-startup element with servlet configuration in web.xml file or use WebServlet annotation loadOnStartup variable to tell container to load the servlet on system startup.
The load-on-startup value should be int, if it’s negative integer then servlet container will load the servlet based on client requests and requirement but if it’s 0 or positive, then container will load it on application startup.
If there are multiple servlets with load-on-startup value such as 0,1,2,3 then lower integer value servlet will be loaded first.
Did you know it?
14
How can we create deadlock condition on our servlet?
Answer
one simple way to call doPost() method inside doGet() and doGet()method inside doPost() it will create deadlock situation for a servlet.
Did you know it?
15
How many objects are created when same or different client send 100 requests to a servlet?
Answer
Servlet creates only one instance either request is 100 or 200.
Did you know it?
16
What are Wrappers in servlet?
Answer
Wrappers are sandwiched between servlet classes and servlet environments by wrapping servlet APIs. Wrappers allow to add additional functionalities to the request and response object given by the servlet container.
Filters can also use wrappers to wrap request or response.Servlet 3.0 provide following 4 wrapper classes. 1. java.serlet.ServletRequestWrapper 2. javax.servlet.ServletResponseWrapper 3. javax.servlet.http.HTTPServletResponseWrapper