Why Servlet ?
Dynamic web applications requires processing of request information, accessing other resources and generating response content dynamically. CGI (Common Gateway Interface) technology was being used to generate dynamic contents.
CGI technology was being used to provide dynamic content to the user. It executes a program that resides in the server. It can access databases to produce relevant dynamic content. Program written in server can be written in native OS language such as C++. CGI can be implemented using any programming language but mostly CGI is implemented in Perl.
Drawbacks of CGI Technology
- For each request CGI server receives, new OS process is created
- As number of requests increases, response time also increases
- Program executed by CGI scripts are written in native language (C, C++ or Perl), so it is platform dependent
Need of better web technology like Servlet, A better web technology was required to overcome the drawbacks of CGI technology. A technology that provide lightweight, robust and portable. Servlet, being developed on Java Technologies, provides all the capabilities of Java and brings ultimate solution to develop dynamic web applications. Servlet is not limited to only web technology, it can be used by any request-response programming model.
What is Servlet ?
Servlet is Web technology which is used to develop server side web components for dynamic web applications. Java Servlet technology provides dynamic, user-oriented content in web applications using a request-response programming model.
A servlet is a Java programming language class 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.
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.
What Servlet does?
- Receives different type of requests(like GET, POST)
- Process the request data
- Prepares HTML response
Web Server => Web Container => Web applications => Web Components
Comparison between Servlet and CGI