1. What is a servlet?
A servlet is a java program that runs within a web server and serves the client request hence the name servlet. Servlets can receive and respond to requests from web clients working on any client-server protocol, but HTTP is predominantly used. Servlet is a server side script technology which can process client request and give back the response.
A servlet container is a container for servlets which is responsible for managing the servlets. The main functions are load, initialize and execute servlets to serve client requests. When a request is received, the container decides what servlet to call based on a configuration file. Servlet container comes installed with the web servers. Examples: Tomcat, Glassfish, JBoss.
3. What is CGI?
Common Gateway Interface(CGI) was the most commonly used for server side scripting before the evolution of the java servlet technology. With traditional CGI, a new process is started for each HTTP request eating up more of the CPU cycles thus degrading the performance of the system.
4. What are the advantages of servlet over CGI?
- When a CGI script is called, a new process is created for each and every client request. But for servlets, each request is handled by a separate thread. Hence, servlet is more efficient because process creation takes more time compared to thread creation.
- For each new CGI requests, CGI script is copied into memory. But with servlets, there is only one copy across requests and is shared among threads.
- In CGI multiple process serves concurrent requests. But for servlets, only single instance serves concurrent requests.
5. Describe about the life cycle of servlet?
Life cycle methods of a servlet are:
- init() method: During initialization web container initializes the servlet instance by calling the init() method passing a ServletConfig interface object as parameter. The ServletConfig object allows the servlet to access initialization parameters. It is called only once in the life cycle of servlet.
- service() method: When client requests are serviced this method gets called. Each requests is serviced in its own thread. For every client requests web container calls the servlet’s service method. The service method determines the type of requests and invokes the appropriate method e.g. if the client request is HTTP get method then corresponding servlet’s doGet() method will be called.
- destroy() method: The web container calls the destroy method and takes the servlet out of service. It is called only once in the life cycle of servlet, when you stop the server or when you undeploy the application from the server.
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-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.