What is a Servlet?

What is a Servlet?

By Eric Burke, OCI Senior Software Engineer

June 1999


The typical definition says that a servlet is a Java class that extends the functionality of a web server, offering an alternative to CGI scripts.

Specifically, the javax.servlet and javax.servlet.http packages make up a standard extension to the Java platform, which allows developers to create servlets.

Servlets are usually used for the following tasks:

Ultimately, servlets allow you to replace static web pages with dynamic web applications.

Advantages of Servlets

Portability

Servlets work on every major web server. You also have the option of running servlets in a standalone "Servlet Engine," such as Live Software's JRun. Since servlets are written in Java, they do not have to be re-compiled to run on different web servers.

High Performance

Contrary to what you might expect, servlets are typically much faster than CGI programs, even if those CGI programs are written in C. This is because servlets are persistent. Once a servlet is loaded into memory, it can be reused by multiple Java threads. Each client request (from a web browser) is handled in a thread, which is much less expensive than creating an entirely new process as CGI requires.

Ease of Development

Servlets are written in Java, therefore all of the familiar Java APIs are available to developers. This allows easy access to relational databases, distributed computing, and portable file I/O. The Servlet API also includes support for parsing HTTP requests from web browsers.

Reliability

Servlets, like other Java programs, run inside of a Java Virtual Machine (JVM) on a server machine. Servlets cannot access random memory locations and benefit from Java's garbage collector, which greatly reduces the chances for memory errors. The Servlet API also provides consistent exception handling, logging, initialization, and cleanup mechanisms. All of these features allow developers to create highly reliable web applications.

When to Use Servlets

Servlets are a good option whenever you wish to add dynamic behavior to a web site. Servlets are faster, easier to develop, and more portable than CGI scripts, so it is hard to argue against them.

When client-side portability is critical, servlets are preferable to applets. Java Applets require your clients to support Java in the web browser, while servlets can generate pure HTML, which is portable to any browser. Applets also suffer performance problems because of slow initial download times.

When special purpose GUI widgets are required, such as wireframe graphics or expandable trees, an applet may be the only option. However, much of your business logic can still reside in a servlet. Since both applets and servlets are written using Java, it is easy to communicate between the two using RMI, sockets, or directly using the HTTP protocol.



Software Engineering Tech Trends (SETT) is a monthly newsletter featuring emerging trends in software engineering.

Check out our most popular articles!

secret