Follow @devglan

servlet Interview Questions and Answers

Each servlet Interview Question Sets contains 12 questions.

1)   What is a servlet?

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.

2)   Explain functioning of web server with an example in context to servlet?

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.

3)   How performance of Servlet is better than CGI?

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.

4)   What is the role of web server in a J2EE application?

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.

5)   What are the functions of Servlet container?

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.

6)   Explain Servlet Workflow.

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.

7)   Can we define costructor in Servlet?

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.

8)   What is difference between GenericServlet and HttpServlet?

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.

9)   What is a deployment descriptor in Servlet?

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.

10)   What is the inter-servlet communication?

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.

11)   Are Servlets Thread Safe? How to achieve thread safety in servlets?

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.

12)   What is difference between ServletResponse sendRedirect() and RequestDispatcher forward() method?

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.

Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.