from Wikipedia, the free encyclopedia

As servlets is called Java - classes whose instances within a Web server to accept requests from clients and answer. The content of the answers can be created dynamically , i.e. at the moment of the request, and does not have to be available statically (for example in the form of an HTML page) for the web server. Servlets represent a further development of the CGI interface or other conceptions originally used in the web area . Java web applications mainly use servlets, while the classic generation of dynamic web content via CGI is done using PHP , Ruby on Rails , Active Server Pages or Perl , for example .

The servlet specification is part of the Java EE technology. Servlet container are thus an integral part of all Java EE - application server . However, a servlet container is not always a complete Java EE implementation. Independent of this , there are web servers that contain a servlet container (e.g. Apache Tomcat , Jetty ).


A servlet is implemented by creating a class that javax.servlet.Servletimplements the interface . There is the class javax.servlet.http.HttpServletthat simplifies this process. Then one or both methods doGetand are doPostoverwritten in order to be able to process the two most important HTTP methods GET and POST . Often, only the method servicethat is called independently of the HTTP command is overwritten. In order to be able to register the servlet on the web server, meta information must be provided. This meta information is stored in an XML file called web.xmlthe deployment descriptor . The deployment descriptor is combined with the compiled servlet class (and any other required classes) in an archive file, the web archive . This web archive is transferred to the servlet container via a functionality provided by it (deployment) .

At runtime, the web server accesses the servlet container, which in turn may create an instance of the servlet and start the corresponding method. Some containers process several requests and thus threads at the same time via a pool of servlets. That is why the simultaneous access of several threads should always be kept in mind for attributes within a servlet.

Frequently, both parameters of the request and session data are used in a servlet , for example to generate a personalized response or to save or change data on the server. In addition to text (for example, HTML or XML), the answers can also be images or other binary files.

Servlets are often used according to the Model View Controller (MVC) design pattern in the form of JavaServer Pages (JSP). The JSP or JSF usually represent the view. Interestingly, the JSP pages are also compiled into servlets at runtime. The self-written servlets represent the controller and model. Frameworks such as B. Spring MVC or Struts form another level of abstraction and offer a more sharply separated MVC pattern.


The English word "servlet" is a suitcase word that is made up of the terms " server " and " applet ", ie server-side applet and thus servlet.

With the introduction of the Java language, Sun defined two types of applications : applications (i.e. real, full-blown desktop applications) and applets (literally application-small , i.e. mini-applications that only run within a container - usually in a web browser ). When Java technology also came to the server side, they looked for a name for the small bits of code that no longer run in the context of the browser but in that of the web server, which is reminiscent of applets. After all, it is a similar concept in the broadest sense: small server applications that need a container (here: a web server extended by a servlet container) around them in order to be able to run. This led to Servlet .

Standardization / versions

The handling, behavior and administration of Java servlets follows a standard that is defined via the Java Community Process (JCP). We are actively working on this standard and there are different versions. The last version released is 4.0, which is part of the Java EE 8 specification. It was released in September 2017.


Separate, executable modules can be written to expand the functionality of a server. These server extensions are available for numerous platforms, e.g. B. the so-called Internet Server API (ISAPI) exists for Microsoft Internet Information Services (IIS ). In Java, server extensions are written using the Servlet API. The server extension modules are called servlets.


  1. The user submits a completed form in the browser.
  2. The browser triggers the associated action by sending the form data to the web server.
  3. The web server translates the action name of the form into the name of a servlet class. To do this, it uses information from the deployment descriptor , a file called "web.xml".
  4. Depending on the HTTP method of the form (GET or POST), the web server calls the doGet or doPost method of the servlet and transfers the request data as parameters together with a response object with which outputs are made can. The outputs are sent from the web server to the browser after the method is completed.
  5. The browser receives the answer and displays it.
  6. The user reads the answer in the browser.

The following flow chart shows a typical process when calling a servlet.

Flow diagram of a web application with servlet

Web links

Individual evidence

  1. JCP specification 369
  2. Java EE 8 Technologies