Monday, April 18, 2011

Servlet Interview Questions

The following are some questions you might encounter with respect to Java Servlets in any Interview. Servlets are the back-bone of almost all J2EE Web Applications and any interviewer who is selecting people for his J2EE project would want a person who is confident & comfortable using them.

Apart from the questions below, there are a few articles that I have put up (as part of the SCWCD Certification series) on Servlets that you might find useful. You can use them to revise/review your understanding of Servlets.

They are:

Servlet History
Servlet Handling of Http Requests
Servlet Request Types
Obtaining a Servlets Initialization Parameters
Servlet Context
Servlet Life-cycle
Servlet Exception Handling
Servlet API

Questions:

1. What is a Servlet?

Java Servlets are server side components that provides a powerful mechanism for developing server side of web application. Earlier CGI was used to provide server side capabilities for web applications. Although CGI played a major role in the explosion of the Internet, its performance, scalability and reusability issues made it less and less desirable among people who wanted enterprise class scalable applications. Java Servlets was the solution to all of CGIs problems. Built from ground up using Sun's java technology, servlets provide excellent framework for server side processing. They are an integral part of almost all J2EE applications

2. What are the types of Servlet?

There are two types of servlets, GenericServlet and HttpServlet. GenericServlet defines the generic or protocol independent servlet. HttpServlet is subclass of GenericServlet and provides http protocl specific functionality.

3. What are the differences between HttpServlet and Generic Servlets?

GenericServlet defines a generic, protocol-independent servlet whereas HttpServlet Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site that uses the Http Protocol.

4. What are the differences between a Servlet and an Applet?

* Servlets are server side components that executes on the server whereas applets are client side components and executes on the web browser.
* Applets have GUI interface but there is no GUI interface in case of Servlets.
* Applets have limited capabilities whereas Servlets are very poweful and can support a wide variety of operations
* Servlets can perform operations like interacting with other servlets, JSP Pages, connect to databases etc while Applets cannot do all this.

5. What are the differences between the doGet and doPost methods?

* Get sends information from the browser to the Servlet as contents appended to the query string in the URL while Post uses hidden variables
* Because of the above point, post is a lot safer than get
* Get has a size limitation - i.e., we can send only approximately 1 Kb of data while Post can send a singnificantly higher amount of data
* Get is the most common type of sending data from a browser to a servlet, while Post is gaining popularity because of its size and safety which is better than the get.

6. What are the different methods present in a HttpServlet?

The methods of HttpServlet class are :

* doGet() - To handle the GET, conditional GET, and HEAD requests
* doPost() - To handle POST requests
* doPut() - To handle PUT requests
* doDelete() - To handle DELETE requests
* doOptions() - To handle the OPTIONS requests and
* doTrace() - To handle the TRACE requests

Apart from these, a Servlet also contains init() and destroy() methods that are used to initialize and destroy the servlet respectively. They are not any operation specific and are available in all Servlets for use during their active life-cycle.


7. What are the advantages of Servlets over CGI programs?

Java Servlets have a number of advantages over CGI and other API's. Some are:

1. Platform Independence - Java Servlets are 100% pure Java, so it is platform independent. It can run on any Servlet enabled web server. For example if you develop an web application in windows machine running Java web server. You can easily run the same on apache web server without modification code. Platform independency of servlets provide a great advantages over alternatives of servlets.

2. Performance - Anyone who has used CGI would agree that Servlets are much more powerful and quicker than CGI. Because the underlying technology is Java, it is fast and can handle multiple request simultaneously. Also, a servlet gets initialized only once in its lifetime and then continues to serve requests without having to be re-initialized again, hence the performance is much higher than CGIs.

3. Extensibility - Java Servlets are developed in java which is robust, well-designed and object oriented language which can be extended or polymorphed into new objects. So the java servlets takes all these advantages and can be extended from existing class the provide the ideal solutions.

Also, in terms of Safety & Security Servlets are superior when compared to CGI.

8. What are the lifecycle methods of Servlet?

The interface javax.servlet.Servlet, defines the three life-cycle methods. These are:

public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()

The container manages the lifecycle of the Servlet. When a new request come to a Servlet, the container performs the following steps:

1. If an instance of the servlet does not exist, the web container
* Loads the servlet class.
* Creates an instance of the servlet class.
* Initializes the servlet instance by calling the init method.
2. The container invokes the service method, passing request and response objects.
3. To remove the servlet, container finalizes the servlet by calling the servlet's destroy method.

9. What are the type of protocols supported by HttpServlet?

It extends the GenericServlet base class and provides an framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol.

10. What is ServletContext?

ServletContext is an Interface that defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine.

11. What is meant by Pre-initialization of Servlet?

When servlet container is loaded, all the servlets defined in the web.xml file do not get initialized by default. When the container receives a request to hit a particular servlet, it loads that servlet. But in some cases if you want your servlet to be initialized when context is loaded, you have to use a concept called pre-initialization of Servlet. In this case, the servlet is loaded when context is loaded. You can specify 1 in between the tag in the Web.xml file in order to pre-initialize your servlet

12. What mechanisms are used by a Servlet Container to maintain session information?

Servlet Container uses Cookies, URL rewriting, and HTTPS protocol information to maintain the session.

13. What do you understand by servlet mapping?

Servlet mapping defines an association between a URL pattern and a servlet. You can use one servlet to process a number of url patterns. For example in case of Struts *.do url patterns are processed by Struts Controller Servlet.

14. What interface must be implemented by all Servlets?

The Servlet Interface must be implemented by all servlets (either the GenericServlet or the HttpServlet)

15. What are the uses of Servlets?

* Servlets are used to process the client requests.
* A Servlet can handle multiple request concurrently and be used to develop high performance system
* A Servlet can be used to load balance among serveral servers, as Servlet can easily forward request.

16. What are the objects that are received when a servlets accepts call from client?

The objects are:
ServeltRequest and
ServletResponse

The ServeltRequest encapsulates the communication from the client to the
server. While ServletResponse encapsulates the communication from the Servlet back to the client. All the passage of data between the client and server happens by means of these request and response objects.

Some more questions on Servlets - Servlet Interview Questions - Part II

If you have any questions that you want answer for - please leave a comment on this page and I will answer them.

If you have any more questions on Servlets that you have faced during your interviews and wish to add them to this collection - pls drop a note to anandvijayakumar007@gmail.com and I shall be glad to add them to this list.

More Java & J2EE Interview Questions with Answers - Click Here
© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers