ITWissen.info - Tech know how online

servlet

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 include the generation of dynamic information. Servlets are part of a web application managed by a web container. Among other things, servlets provide an encapsulation ofCGI 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( PHP).

Concept of servlets

The concept of servlets goes back to James Gosling - one of the founders of the Java programming 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 technique of Java Server Pages( JSP) developed, which avoids the disadvantage of the coupling of visualization and logic existing with Servlets.

The basic idea for the term "servlet" were the so-called applets, which are loaded by the web browser( client). And since servlets are server-side programs, the term servlet is derived from "server" and" applet".

The basic requirement for using 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 lifecycle. Web containers can be part of a web server or an application server. The web container then has the task of forwarding a client 's request to a servlet, which generates a response - for example, a web page in HTML format - using Java code.

Alternative concepts of servlets

However, there is also the alternative possibility that servlets can call each other. One methodology for doing this is the servlet context. The servlet context provides the interface for servlets to their environment and manages data such as values of a web application that are 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 used only with HTTP/web servers. Compared to CGI scripts, servlets offer the advantage that they remain in memory after initial generation, and thus data can be stored between requests. Whereas when using CGI scripts, each request is handled in a new process.

One aspect in the creation of servlets are the topics concurrency and distribution; there is only one instance per servlet but possibly several clients with requests. Or servlets working together competitively access common resources such as databases. 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, where 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 such as 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 are usable.
However, the disadvantage is that with servlets the visualization and the program logic are not separated. This means that if 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 several license-free web servers with servlet functionality that comply with the Java servlet specification published by 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 server.

Glashfish Powerful web server with support for servlets, CGI scripts, and PHP that integrates a web container.

Jetty HTTP server that can be embedded in custom software(web services), among other things.

The listed links can be used to access lists from the Sun company with further servlet-capable servers.

Informations:
Englisch: servlet
Updated at: 28.10.2013
#Words: 766
Links: software (SW), server, Java, source code, hypertext markup language (Internet) (HTML)
Translations: DE
Sharing:    

All rights reserved DATACOM Buchverlag GmbH © 2024