A servlet is a Java program that runs in a Web server. The servlet takes an HTTP request from a browser, generates dynamic content like querying a database, and provides an HTTP response back to the browser. Alternatively, the servlet can be accessed directly from another application component or send its output to another component. A servlet runs in a J2EE application server. Servlets are one of the main application component types of a J2EE application, along with JavaServer Pages (JSP) and EJB modules, which are also server-side J2EE component types.
Prior to servlets, Common Gateway Interface (CGI) technology was used for dynamic content, with CGI programs being written in languages such as Perl and being called by a Web application through the Web server.
Disadvantages of CGI:-
1. For each request CGI Server receives, It creates new Process.
2. If the number of requests from the client increases then more time it will take to respond to the request.
3. As programs executed by CGI Script are written in the native languages such as C, C++, perl which are platform independent.
Advantages of Servlets:-
1. Servlets provide a way to generate dynamic documents that is both easier to write and faster to run.
2. Servlet technology, in addition to improved scalability, offers the well-known Java advantages of security, robustness, object orientation, and platform independence.
3. Servlets are fully integrated with the Java language and its standard APIs, such as JDBC for Java database connectivity.
4. Servlets are fully integrated into the J2EE framework, which provides an extensive set of services that your Web application can use, such as Java Naming and Directory Interface (JNDI) for component naming and lookup, Java Transaction API (JTA) for managing transactions, Java Authentication and Authorization Service (JAAS) for security, Remote Method Invocation (RMI) for distributed applications, and Java Message Service (JMS). The following Web site contains information about the J2EE framework and services:
5. A servlet handles concurrent requests, and servlets have a well-defined lifecycle.
6. The servlet request and response objects offer a convenient way to handle HTTP requests and send text and data back to the client.
Diagrammatic Representation of Servlet:-
Servlet 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.