1. Web Application.
- Web application exists everywhere in the internet or intranet. You nearly use them every day through browsers.
- 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.
- This is the most important web component in building web applications.
- It runs in a web container on the server-side and generates a 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.
2.1 Servlet Container.
- Servlet is executed in a container. The container provides a run-time environment that the servlet needs.
- The most popular web container include Tomcat, Jetty. They are all open-source and free.
2.2 Servlet Advantages.
- 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.
- 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.
- 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.
- 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.
- CGI is the abbreviation of Common Gateway Interface. It is widely used to build web applications before the java servlet is invented.
- 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.
- Below are the steps of a CGI request-response process.
- The user clicks a page URL that points to a CGI program.
- The web server receives the request and checks which program to execute by the URL and the request parameters.
- Web server executes the requested CGI program in a separate OS process.
- The CGI program gives response data back to the webserver.
- The web server wraps the response data in an HTTP response and sends it back to the client.
- The below diagram shows how the CGI process user request and send a response back to the client user.
3.1 CGI Disadvantages.
- 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.
- 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.
- Poor Portability: CGI is written use C. C++ etc. They are not portable program languages. So CGI is operating system-dependent.
- After the above comparison of Servlet and CGI, we prefer to use Servlet to develop web applications.