Monday, October 12, 2015

Servlet Interview Questions and Answers

6. Explain MVC design pattern?
Model-View-Controller (MVC) is a design pattern which divides a software application into three parts namely model, view and controller.
  • A model deals with behavior of the application. It contains the data and business logic of the application. It notifies views and controllers when there is change in its state.
  • A view renders the information to the user so that it looks attractive and appealing. It takes information from the model using which it generates output.
A controller takes input from user and sends command to model or view. It controls the flow of application.
7. Explain the web application directory structure?
web application’s directory structure has document root which contains JSP files, HTML files, JavaScript files and static files such as image files. There is a directory WEB-INF under the document root which contains configuration files related to application. This configuration files can’t be served directly to the client. The WEB-INF directory contains:
  • /WEB-INF/classes/* : It contains compiled class files
  • /WEB-INF/lib/*.jar : It contains different library jar files
  • /WEB-INF/web.xml : It is a deployment descriptor that specify the web application configuration.
8. what are the two packages that deal with servlets?
Servlet API exist in two packages:
  • Javax.servlet: The classes and interfaces in javax.servlet are not protocol dependent. e.g. It can support different protocols like HTTP, FTP.
  • Javax.servlet.http: The classes and interface in this package are specific for requests using HTTP protocol. Some of the classes and interfaces in this package extend those of javax.servlet package.
9. Explain servlet class hierarchy?
  • Interface Servlet: It declares the standaed API’s which all the servlet implementations needs to define. All servlet classes must implement this interface directly or indirectly.
  • Class GenericServlet: It defines a generic, protocol independent servlet. Servlet developed to cater NON-HTTP requests.
  • Class HTTPServlet: Servlet used to cater client HTTP requests.
10. What is Servlet interface?
The Servlet interface is the abstraction of Java Servlet API. It declares the life cycle methods of a servlet. All the servlet classes must implement it either directly or indirectly by extending a class which implements the Servlet interface. E.g. HTTPServlet, GenericServlet.

11. What is destroy() method of Servlet interface?
It is invoked by the servlet container to indicate that the servlet is taken out of service.


12. What is init() method of Servlet API?

It is called by the servlet container to indicate to a servlet that the servlet is being placed into service. Init() method takes ServletConfig object as an argument. ServletConfig object contains initialization and startup parameters for the servlet.
13. What is service() method of Servlet API?
It is invoked by the servlet container to allow the servlet to respond to a request.


14. What is HTTPServlet?

HTTPServlet is an abstract class. Any servlet that works on the HTTP should extend this class. This class contains implementations of methods that are specific to HTTP. It contains doXXXX methods which are known as the service methods. All the servlets extending this class should implement any one of the doXXXX() methods. E.g. doGet(), doPost().

15. List different types of HTTP requests?

get, post, head, options, put, trace, delete.
16. List the do-XXXX methods in HTTPServlet?
void doGet(HTTPServletRequest req, HTTPServletResponse res)void doPost(HTTPServletRequest req, HTTPServletResponse res)void doHead(HTTPServletRequest req, HTTPServletResponse res)void doOptions(HTTPServletRequest req, HTTPServletResponse res)void doPut(HTTPServletRequest req, HTTPServletResponse res)void doTrace(HTTPServletRequest req, HTTPServletResponse res)void doDelete(HTTPServletRequest req, HTTPServletResponse res)
17. Explain briefly the doGet and doPost methods in HTTPServlet?
Void doGet(HTTPServletRequest req, HTTPServletResponse res)
Above method is called by the server (via service method) to allow a servlet to handle a GET request. It will be called when a URL is requested or the form method is GET.
Void doPost(HTTPServletRequest req, HTTPServletResponse res)
It is called by the server (via service method) to allow a servlet to handle a POST request. It will be called when the form method is POST.

No comments: