· Servlets are used primarily with web servers, where they provide a Java-based replacement for CGI scripts. They can be used to provide dynamic web content like CGI scripts.
· Advantages of servlets over CGI scripts:
1. Servlets are persistent between invocations, which dramatically improves performance relative to CGI programs.
2. Servlets are portable among operating systems and among servers.
3. Servlets have access to all the APIs of the Java platform (e.g. a servlet can interact with a database using JDBC API).
Servlets are a natural fit if you are using the web for enterprise computing. Web browsers then function as universally available thin clients; the web server becomes middleware responsible for running applications for these clients.
Thus the user makes a request of the web server, the server invokes a servlet designed to handle the request, and the result is returned to the user in the web browser. The servlet can use JNDI, Java IDL, JDBC, and other enterprise APIs to perform whatever task is necessary to fulfill the request.
Servlets can be used when collaboration is needed between people. A servlet can handle multiple requests concurrently, and can synchronize requests. So servlets can support on-line conferencing. Servlets can forward requests to other servers and servlets. Thus, servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task type or organizational boundaries.
The Servlet Life Cycle
When a client (web browser) makes a request involving a servlet, the web server loads and executes the appropriate Java classes. Those classes generate content (e.g. HTML), and the server sends the contents back to the client. From the web browser’s perspective, this isn’t any different from requesting a page generated by a CGI script, or standard HTML. On the server side there is one important difference is persistence. Instead of shutting down at the end of each request, the servlet remains loaded, ready to handle the subsequent requests. Each request is handled by a separate thread. These threads share code and data (instance vars). So try to avoid using instance vars, else be sure to use them in synchronized blocks.
The request processing time for a servlet can vary, but is typically quite fast when compared to a similar CGI program. The advantage in the servlet is that you incur the most of the startup overhead only once.
When a servlet loads, its init() method is called. You can use init() to create I/O intensive resources, such as database connections, for use across multiple invocations. If you have a high-traffic site, the performance benefits can be quite dramatic.
The servlet’s destroy() method can clean up resources when the server shuts down.
Since a servlet remains active, it can perform other tasks when it is not servicing client request, such as running a background thread (where clients connect to the servlet to view the result) or even acting as an RMI host, enabling a single servlet to handle connections from multiple types of clients.