First of all let's get some knowledge about servers. Generally, there are two types servers, namely:
- Web Server: To run web applications like Facebook. An example of a web server is Apache tomcat.
- Application Server: To run enterprise applications, like a bank's website. Example: JBoss, Glassfish.
There are total 40+ servers in Java.
There is one server in .Net (IIS).
There is one server in PHP (Wampp).
A web Server contains
Servelt Container/ Engine
In a simple situation, a user types a URL in a browser and gets a web page to read. Here the server only sends a web page to the client.
The basic purpose of a Servlet container is to generate a web page dynamically on the server side.
A Servlet container is an API (collection of group of classes and interfaces) that contains a Servlet that generates a response to the client's request.
Common Gateway Interface (CGI)
The Common Gateway Interface (CGI) is a standard way for a Web server to a Web user's request to an application program and to receive data back to forward to the user. So, what is the difference between a Servlet and CGI? The differences are:
- Each CGI request starts a new process that results in slow processing of the client's request.
- CGI is a platform dependent language, for example C, C++, and so on.
Servlet
A Servlet is an interface defined in a javax.servlet package. It declares three essential methods for the life cycle of a servlet, init(), service() and destroy().
- When the servlet is called for the first time, the Servlet Container loads the servlet class and calls its init() method.
- The service() method is invoked upon each request after its initialization. The web container calls the service() method of the servlet for every request. The service() method determines the kind of request being made and dispatches it to an appropriate method to handle the request.
- Finally, it calls the destroy method when the servlet object should be destroyed. It releases the resources being held.
How a web server processes a request
- A HTTP request is received by the web server.
- It forwards the request to the servlet container.
- The container invokes the init() method of the servlet for initialization (invoked once when the servlet is loaded the first time).
- It then invokes the service() method of the servlet to process the HTTP request, in other words it reads the data in the request and formulates a response.
- The web server returns the dynamically generated results to the correct location.
Advantages of Servlet
- Secure: Because it uses Java Language.
- Robust: Because Servlets are Managed by JVM.
- Portability: Write once Run anywhere property because it uses Java Language.
- Efficiency: Servlet invocation is highly efficient as compared to CGI.
- Extensibility: The Servlet API is designed in such a way that can be easily extended.
- Inexpensive: There are a number of free web servers available for personal use.
We will explain how to write programs in a servlet in my next article.