Servlet Interview Questions

Servlet is very basic and important technology to learn before starting work with any J2EE application development framework. While preparing for Java/J2EE interviews, everyone must get a fair idea about servlet interview questions. You can get servlet questions in any J2EE interview even if you have not mentioned it in your resume. I am writing here the top 10 servlet interview questions, which every J2EE programmer must know. 

1.) What is a servlet? How many types of servlet are there?

Servlet is a java program that extends the capabilities of a web server. Servlet process the requests coming from clients and return back the dynamically generated response.

Servlets are created by implemented the Servlet interface directly or indirectly. Normally servlets are defined of two types. One is Generic servlet and second is HTTP servlet.

Generic servlets implements Servlet interface and these are not protocol specific.
HTTP servlets implements HTTPServlet interface and these are HTTP protocol specific.

HTTP  Protocol - Hyper Text Transfer Protocol.

2.) What is servlet container?

A servlet container also called web container is a web server's component with handles the servlet life cycle. Servlet container also manages the servlet URL mapping and provides services like transaction management, security, concurrency and deployments etc. services.

3.) Explain servlet life cycle.

Servlet lifecycle is handed by servlet container in which servlet is deployed. Servlet lifecycle start from loading the class and ends at servlet destroy.

  • On first request, servlet container checks for the servlet instance. If instance is not available than container loads the servlet class and creates an instance.
  • Initialise the servlet instance by calling servlets init() method. Servlets init() only called once in servlets life cycle. This init() method is used to configure the servlet's instance.

public void init() throws ServletException {
// Initialisation code here
}
  • After single time initialisation of servlet, service() method of servlet class is called on each request. Request processing is done in service method.

public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// service code here
}
  • Finally servlet container calls the servlets destroy() method to remove the instance. Destroy method is used to do the clean up task.  
public void destroy() {
// destroy code here
}

4.) What is difference between servlet config and servlet context?

ServletConfig - 
  • Servlet container will create separate ServletConfig object for each servlet.
  • ServletConfig object will be created during initialisation step of servlet life cycle. 
  • ServletConfig object holds all the configuration information of the servlet provided in web.xml file under servlet mapping in  <init-param> 
  • ServletConfig also provides methods to get these configuration details.
  • ServletConfig object will be destroyed with servlet destroy.

ServletContext
  • Only one ServletContext object will be created for whole web application.
  • ServletContext will be created at deployment time.
  • ServletContext is shared between all servlets and JSP's in web applications, So if some information needs to be passed between two servlets it can be done by putting the information in ServletContext from one servlet and than access the same from other servlet.
  • Servlet context will be destroyed on un-deployment of application from servlet container.
  • ServletContext takes information from <context-param> tag of web.xml.
5.) What is <load-on-startup> attribute in servlet mapping? What is early binding ?

In normal scenario servlet will be loaded in container on its first request, So first request will be a little bit slow.
But if we want to load the servlet on application deployment to make the first request faster, than it can be done by specifying <load-on-startup> in servlet configuration in web.xml. 
If the value is 0 or +ve (>0), container will loads the servlet on application start up. Servlet with lower +ve value will be loaded first. If two or more servlets have the same load on startup value than the will be loaded in the sequence they are configured in web.xml.
Servlet with -ve (<0) load on startup value will be loaded on first request.

6.) What will happen if servlets destroy() method is called from servlet's init() method?

Servlet will work in normal way, because destroy method is a normal method(used to write cleanup code before destroy)  which is called by servlet container before destroying the servlet.

7.) What is session? How to manage session in servlet?

Session - Session is a specific time interval.

HttpServlet works on HTTP (Hyper Text Transfer Protocol). HTTP is a state less protocol, So after one request completion it will not recognize if the second request is coming from same client or different. That is the reason we need to track the session in servlet java applications.

In servlet session can be managed in different ways. Below are the ways to manage session in java servlet application.
  1. Session management with Cookies.
  2. Session management with Hidden Form Fields.
  3. Session management with URL Rewriting.
  4. Http Session.

8.) What is servlet collaboration?

Servlet collaboration is communication between servlets to share the information or control between them. There are various methods available for servlet collaboration to share information.

  • Use RequestDispatcher's include() method to include other server response or forward() methods to transfer control to other servlet or JSP.
  • Use sendRedirect() method of HttpServletResponse. It will create a fresh new request and transfer the control.
  • Use system properties. We can set information in one servlet by setter and that can be used in another servlet with getter method of System class. 
  • Use ServletContext's setAttribute() and getAttribute() methods.
  • Use any application scoped shared class.
9.) How to make servlet thread-safe? What is SingleThreadModel?

By default servlet is not thread safe. For each coming request a new servlet container creates a new thread to handle that.
Servlet class can be made thread safe by implementing SingleThreadModel, which will guarantees that all request will be processed by one servlet instance in serial manner.
But SingleThreadModel class was deprecated after servlet version 2.4. 
Secondly we can use synchronization to make our servlet class thread safe.

These are the most important servlet questions asked in interviews. I something is missed please put the questions in comment, I will update the list.


No comments:

Post a Comment