Servlet Overview

The website consists of static resources and dynamic resources. Static resources include Html page, javascript, CSS, images, etc. And dynamic resources include CGI, Servlet, JSP, etc. In this article, we will give you a general explanation about Web Applications, J2EE Servlet, and CGI. We will also compare them to point out the advantage and disadvantages of using them.

1. Web Application.

  1. Web application exists everywhere in the internet or intranet. You nearly use them every day through browsers.
  2. A web application generally includes the following components such as JSP, Servlet, Html pages, javascript, images.
  3. JSP and Servlet are the dynamic web resources that run in the web container and generate data that will send back to the client request.

2. Servlet.

  1. This is the most important web component in building web applications.
    web-application-servlet-architecture
  2. It runs in a web container on the server-side and generates a dynamic page based on the request.
  3. It is based on Java, so it is secure, strong, portable, and extendable.
  4. 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.

2.1 Servlet Container.

  1. Servlet is executed in a container. The container provides a run-time environment that the servlet needs.
  2. The most popular web container include Tomcat, Jetty. They are all open-source and free.

2.2 Servlet Advantages.

  1. Portability: Because based on Java. So it can be deployed and 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.
  2. Efficiency: When servlet container startup, it loads the servlet class and creates an instance of that class waiting for the request. When a request comes in, the container creates a thread that will run the servlet code to respond to the request. This is very efficient in multiple concurrent requests because each request is processed by one thread. We all know the thread is less resource consumption than process. Servlet will only use the resources managed by it’s parent process.
    java-servlet-multiple-thread-example
  3. Focus: Because running inside the container, so servlet does not need to care about a lot of low-level issues. Such as data share between requests, 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, the servlet can only focus on the logic or page dispatching.
  4. Security: Because running inside the 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 runs into an error, you can also catch that error as an exception thrown by the container.

3. CGI.

  1. CGI is the abbreviation of Common Gateway Interface. It is widely used to build web applications before the java servlet is invented.
  2. When a web server receives 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.
  3. Below are the steps of a CGI request-response process.
  4. The user clicks a page URL that points to a CGI program.
  5. The web server receives the request and checks which program to execute by the URL and the request parameters.
  6. Web server executes the requested CGI program in a separate OS process.
  7. The CGI program gives response data back to the webserver.
  8. The web server wraps the response data in an HTTP response and sends it back to the client.
  9. The below diagram shows how the CGI process user request and send a response back to the client user.
    process-http-request-by-cgi-program

3.1 CGI Disadvantages.

  1. Poor Efficiency: Because CGI runs in it’s own process, so the resource consumption will be high and the response time will be long. If multiple requests arrive at the same time then even the entire server will be very slow.
  2. Unsafe: Since runs in a 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.
  3. Poor Portability: CGI is written use C. C++ etc. They are not portable program languages. So CGI is operating system-dependent.

4. Conclusion.

  1. After the above comparison of Servlet and CGI, we prefer to use Servlet to develop web applications.

Subscribe to receive more programming tricks.

We don’t spam!

Subscribe to receive more programming tricks.

We don’t spam!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.