Servlet Overview

Website consists of static resources and dynamic resources. Static resources include html page, javascript, css, images etc. And dynamic resources includes CGI, Servlet, JSP etc. In this article we will give you a general explanation about Web Applicaion, J2EE Servlet and CGI. We will also compare them to point out the advantage and disadvantage of using them.

Web Application

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

Servlet

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.

web application servlet architecture

Servlet Container

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.

Servlet Advantages 

  1. Portability
    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.
  2. Efficiency
    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.
    java servlet multiple thread example
  3. Focus
    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.
  4. Security
    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.
READ :   Servlet RequestDispatcher And SendRedirect

CGI

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.

  1. User click a page url which point to a CGI program.
  2. Web server receive the request and check which program to execute by the url and it’s parameter.
  3. Web server execute the requested CGI program in a separate OS process.
  4. The CGI program give response data back to web server.
  5. Web server wrap the response data in a HTTP response and send it back to client.

process http request by cgi program

CGI Disadvantages

  1. 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.
  2. Unsafe
    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.
  3. 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.

 

(Visited 64 times, 1 visits today)

Leave a Reply

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