- Tech know how online


A servlet is software on the web server, written in Java source code, that generates HTML result pages. This extends the capabilities of Web servers to generate dynamic information. Servlets are part of a Web application that is managed by a Web container. Among other things, servlets provide an encapsulation of CGI details. Thus, they offer an alternative to conventional CGI scripts or the generation of dynamic, interactive web pages using, for example, Ruby on Rails, Active Server Pages (ASP) or Hypertext Preprocessor


Concept of Serv

letsThe concept of servlets goes back to James Gosling - one of the founders of the Javaprogramming language - who in 1994 developed the first web server built entirely on Java, and in this context defined the concept of servlets. Somewhat later, the technology of Java Server Pages (JSP) developed, which avoids the disadvantage of coupling visualization and logic that

exists withservlets.

Thebasic idea for the term "servlet" was the so-called applets

that areloaded by the web browser (client). And since servlets are server-side programs

, the term servlet is derived from "server" and "applet". Thebasic prerequisite for the use of servlets is a servlet-capable application or web server, such as all Java EE application servers are. A servlet is a special Java class that is instantiated within a web container. A web container provides the runtime environment for servlets and handles their state management. This includes the communication of the servlet to the outside world as well as the management of its life cycle

. Web containers can be part of a Web server or an application server. Theweb container then has the task of forwarding a client's request to a servlet, which uses Java code to generate a response - for example, a web page in HTML format

Alternative concepts of serv

letsHowever, there is also the alternative possibility that servlets can call each other. One methodology for doing this is the servlet context, which interfaces servlets to their environment and manages data, such as values of a Web application, that is available to all associated servlets. The data can be made available, for example, via a so-called deployment descriptor - a special xml file web.xml. Further possibilities of communication between servlets are special sessions and requests as well as common superclasses.

Client-Servlet Communication Structure

Client-Servlet Communication Structure

Servlets are theoretically not bound to a specific server protocol such as HTTP, but are usually only used with HTTP/web servers. Compared to CGI scripts, servlets have the advantage that they remain in memory

after they have been generated for the first time, and can therefore store data between requests. One aspect of creating servlets is concurrency and distribution; there is only one instance per servlet, but there may be multiple clients with requests.

Or servlets working together access shared resources such as databases in a concurrent manner. In this case, the accesses must be synchronized or it is the task of the web container to provide the synchronization. For this purpose there are the approaches:

  • Request serialization, whereby a queue is formed for requests.
  • Instance Pooling, where a pool of servlet instances is created.

Advantages of Servlets

Compared to CGI scripts, servlets offer considerable advantages:
  • Portable and platform-independent,
  • Java has comprehensive servlet interfaces(APIs),
  • Versatile use of Java, e.g. connection to databases, middleware,
  • Use of the possibilities of an object-oriented programming language like Java for complex tasks,
  • Higher performance through data caching and the processing of requests in threads and not in processes,
  • Scalability through distributed systems,
  • Differentiated security settings such as sandbox or JVM"policy" file are possible due to running in containers, and
  • J2EE security models can be used.
However, the disadvantage is that with servlets the visualization and the program logic are not separated. Thus, when the design of a web application is changed, the program code

must also be modified, which can be correspondingly time-consuming and error-prone.There are various license-free web servers with servlet functionality that fulfill the Java servlet specification published by the company Sun:

Apache Tomcat. A product of the Apache Software Foundation with open source code

. Defines an environment for running Java code on web servers, integrates a servlet container and also an HTTP serverGlashfish

. Powerful web server with support for servlets, CGI scripts, and PHP that integrates a web containerJetty. HTTP server that can be embedded in individual software ( web services

), etc. Youcan access lists of other servlet-capable servers from Sun via the listed links


Informationen zum Artikel
Englisch: servlet
Updated at: 28.10.2013
#Words: 1044