Table of Contents
What is Servlets?
Servlet technology is used to create a web application.
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
Servlet only uses Java as a programming language that makes it platform independent and portable.
Servlet allows us to take advantage of the object oriented programming features of java.
Properties of Servlets :
- Servlets work on the server-side.
- Servlets are capable of handling complex requests obtained from web server.
The javax.servlet
and javax.servlet.http
packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet
interface, which defines lifecycle methods. When implementing a generic service, you can use or extend the GenericServlet
class provided with the Java Servlet API. The HttpServlet
class provides methods, such as doGet
and doPost
, for handling HTTP-specific services.
Architecture of Servlets
Execution of Servlets :
Execution of Servlets involves six basic steps:
- The clients send the request to the web server.
- The web server receives the request.
- The web server passes the request to the corresponding servlet.
- The servlet processes the request and generates the response in the form of output.
- The servlet sends the response back to the web server.
- The web server sends the response back to the client and the client browser displays it on the screen.
The Servlet technology is similar to other Web server extensions such as Common Gateway Interface(CGI) scripts and Hypertext Preprocessor (PHP). However, Java Servlets are more acceptable since they solve the limitations of CGI such as low performance and low degree scalability.
What is CGI?
- CGI (Common Gateway Interface) is a standard way of running programs from a Web server.
- CGI programs are used to generate pages dynamically or to perform some other action when someone fills out an HTML form and clicks the submit button.
- AOLserver provides full support for CGI v1.1.
- CGI program can be written in any programming language that makes it mostly platform dependent as not all programming languages are platform-independent.
Basically, CGI works like this:A reader sends a URL that causes the AOLserver to use CGI to run a program. The AOLserver passes input from the reader to the program and output from the program back to the reader. CGI acts as a “gateway” between the AOLserver and the program you write.
Why Servlets are more efficient the CGI?
Server has to create a new CGI process for every client request. For example, If 100 users are accessing the web application, then the server has to create 100 CGI processes to handle the request made by them. Since a server has limited resources, creating a new process every time for a new request is not a viable option, this imposed a limitation on the server, due to that the server cannot handle more than a specified number of users at the same time.
CGI programs are handled by a new process every time a new request has been made. Unlike CGI, the servlet programs are handled by separate threads that can run concurrently more efficiently.
Read more about CGI at www.oreilly.com