A Java servlet is a Java program that extends the capabilities of a server. Although servlets can respond to any types of requests, they most commonly implement applications hosted on Web servers. Such Web servlets are the Java counterpart to other dynamic Web content technologies such as PHP and ASP.NET.

A Java servlet processes or stores a Java class in Java EE that conforms to the Java Servlet API, a standard for implementing Java classes that respond to requests. Servlets could in principle communicate over any client–server protocol, but they are most often used with the HTTP protocol. Thus "servlet" is often used as shorthand for "HTTP servlet". At Unicode Systems, we use a servlet to add dynamic content to a web server using the Java platform. The generated content is commonly HTML, but may be other data such as XML. Servlets can maintain state in session variables across many server transactions by using HTTP cookies, or URL rewriting.

To deploy and run a servlet, we use a web container. A web container (also known as a servlet container) is essentially the component of a web server that interacts with the servlets. The web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights.

The Servlet API, contained in the Java package hierarchy javax.servlet, defines the expected interactions of the web container and a servlet.

Advantages of using Servlets:

  • Servlets provide a way to generate dynamic documents that is both easier to write and faster to run.
  • Servlets provide all the powerfull features of JAVA, such as Exception handling and garbage collection.
  • Servlet enables easy portability across Web Servers.
  • Servlet can communicate with different servlet and servers.
  • Since all web applications are stateless protocol, servlet uses its own API to maintain session.

Disadvantages of using Servlets:

  • Designing in servlet is difficult and slows down the application.
  • Writing complex business logic makes the application difficult to understand.
  • You need a Java Runtime Environment on the server to run servlets.