A Java servlet is a Java program that extends the capabilities of a server. Although servlets can respond to any type of request, it is most common to deploy applications hosted on web servers. These web servlets are the Java counterpart for other dynamic web content technologies like 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 are mostly used with the HTTP protocol. Thus, "servlet" is often used as an abbreviation for "HTTP servlet". Therefore, a software developer can 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 like XML. Servlets can maintain the state of session variables in many server transactions by using HTTP cookies or URL rewriting.
To implement and run a servlet, you must use a web container. A web container (also known as a servlet container) is essentially the component of a web server that interacts with servlets. The web container is responsible for managing the servlet lifecycle, assigning a URL to a particular servlet, and ensuring that the URL requester has the correct access rights.
The Servlet API, contained in the javax.servlet Java package hierarchy, defines the expected interactions of the web container and a servlet. A servlet is an object that receives a request and generates a response based on that request. The basic Servlet package defines Java objects to represent servlet requests and responses, as well as objects to reflect configuration parameters and the servlet execution environment. The javax.servlet.http package defines the HTTP-specific subclasses of generic servlet elements, including session management objects that track multiple requests and responses between the web server and a client. Servlets can be packaged in a WAR file as a web application.
Servlets can be automatically generated from JavaServer Pages (JSP) using the JavaServer Pages compiler. The difference between servlets and JSPs is that servlets typically include HTML within Java code, whereas JSPs incorporate Java code into HTML. While direct use of servlets to generate HTML (as shown in the following example) has become rare, the high-level MVC web framework in Java EE (JSF) still explicitly uses servlet technology for handling requests / responses of low level through FacesServlet. A somewhat older application is to use servlets in conjunction with JSPs in a pattern called "Model 2", which is a taste of the model-view-controller. The current version of Servlet is 3.1. This is the updated version.