Java Servlet

As servlets are called Java classes that accept their instances within a web server requests from clients and answer. The content of the responses can dynamically, ie, at the moment created by the query are static and do not have already be available to the web server (in the form of an HTML page ). Servlets thus represent the Java equivalent of CGI scripts or other concepts with which dynamic web content can be created (PHP, Ruby on Rails, Active Server Pages, etc.).

Servlets are actually part of the Java EE technology, but there are also independent of Web servers that contain a servlet container (eg Apache Tomcat, Jetty ). Servlet container are an integral part of all Java EE application server.

Use

A servlet is implemented by a class is created that implements the interface javax.servlet.Servlet. There is the class javax.servlet.http.HttpServlet that simplifies this process. Then one or both doGet and doPost is overridden to handle the two major HTTP methods GET and POST can. Often, only the service method is overridden, which is called regardless of the HTTP command. To use the servlet on the Web server, meta information to be provided. This meta information is stored in an XML file named web.xml, called the deployment descriptor. The deployment descriptor is merged together with the compiled servlet class (and possibly other required classes) in an archive file, the web archive. This web archive is passed to the servlet container via a functionality provided by him ( deployment ). At runtime, the Web server accesses the servlet container, which in turn creates an instance of the servlet and start the appropriate method.

In a servlet both parameters of the request and session data is often used to produce, for example, a personalized answer or store data on the server or to change. The answers, in addition to text (for example, HTML or XML ) may also be images or other binary files.

Servlets are often used according to the design pattern Model-View- Controller (MVC) in the form of Java Server Pages, the JSP represents the view. Frameworks such as Spring MVC or Struts complete while the MVC pattern.

Term

The English word " servlet " is a portmanteau composed of the words " server" and " applet ", ie server-side applet and servlet so.

Sun has two types of applications defined with the introduction of the Java language: Applications ( ie proper, full-blown desktop applications ) and applets (literally, application -chen, ie small mini- applications that only within a container - usually in a web browser - expire). When the Java technology came on the server side, they sought there a name for the small code snippets that are now no longer run in the context of the browser, but in which the Web server that is reminiscent of the applet. Finally, there is a broadly similar concept: small server application surfaces that a container (in this case an extended to a servlet container web server) need around them, in order to run. This one came on servlet.

Standardization / versions

The handling, the behavior and Managing Java servlets follow a standard that is defined by the Java Community Process (JCP ). At this standard are working actively and there are different versions. The final published version is 3.1, which is part of Java EE 7 specification. It was published in May 2013.

Demarcation

To extend the functionality of a server separate executable modules can be written. This Server Extensions are available for many platforms, such as exists for the Microsoft Internet Information Services ( IIS), the so-called Internet Server API ( ISAPI). In Java server extensions are written using the Servlet API. The server extension modules are called servlets.

Example

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

432254
de