This is the most important web component in building web applications. It runs in web container at server side and generate dynamic page based on the request. It is based on Java, so it is secure, strong, portable and extendable. A lot of classes and interfaces are provided in it’s API like ServletRequest, ServletResponse , GenericServlet, HttpServlet. You can use them to implement what you want in your java code.
Servlet is executed in a container. The container provide run time environment that servlet needs. The most popular web container include Tomcat, Jetty. They are all opens source and free.
Because based on Java. So it can be deployed an run in multiple operating systems and platforms that support Java. You can write and test the java code use Tomcat in Windows and then deploy it to a Tomcat in Linux.
When servlet container startup, it loads the servlet class and create an instance of that class waiting for the request. When a request come in, the container create a thread which will run the servlet code to response to the request. This is very efficiency in multiple concurrent requests, because each request is processed by one thread. We all know thread is less resource consumption than process. Servlet will only use the resources managed by it’s parent process.
Because running inside the container, so servlet do not need to care about a lot of low level issues. Such as data share between request, database connection pooling, session management, file paths translation, action logging, authorization check and MIME type mapping etc. All these issues will be handled by the container, servlet can only focus on the logic or page dispatching.
Because running inside container, so the container will provide security insurance for it. And it is pure java, so it also inherits the java security such as memory security, strong type safety. If your java code run into an error, you can also catch that error as an exception thrown by the container.
CGI is the abbreviation of Common Gateway Interface. It is widely used to build web applications before java servlet is invented. When web server receive a client request, it will execute an independent external program and transfer the HTTP request data to it. So each request will create a new process.
Below is the steps of a CGI request response process.
- User click a page url which point to a CGI program.
- Web server receive the request and check which program to execute by the url and it’s parameter.
- Web server execute the requested CGI program in a separate OS process.
- The CGI program give response data back to web server.
- Web server wrap the response data in a HTTP response and send it back to client.
- Poor Efficiency
Because CGI run in it’s own process, so the resource consumption will be high and the response time will be long. If multiple request arrive at the same time then even the entire server will be very slow.
Since run in separate process, so it is not secure because of memory leak, dead loop or other program issues. There is not a container to handle such issues.
- Poor Portability
CGI is writen use C. C++ etc. They are not portable program language. So CGI is operating system dependent.
After above compare of Srevlet and CGI, we prefer to use servlet to develop web applications.