Servlet API specification

Java Servlet technology is providing Web developers with simple mechanism for enlarging the functionality of a Web server and for accessing existing business systems.

A servlet could almost be thought of as an applet that runs on the server side--without a face. Java servlets make many Web applications possible. Servlets are modules of Java code that run in the server application to answer the request of the clients.

Servlets are not tied to a specific client-server protocol, though servlets are most often used with HTTP and the word "Servlet" is often associated with the meaning of "HTTP Servlet". The extention of server-side Java applications is the most exciting trend in Java programming. Java language was originally intended for use in small, embedded devices.

It was first introduced as a language for developing elaborate client-side web content in the form of applets. Until recently, Java's potential as a server-side development platform had been sadly overlooked.

Now, Java is coming into its own as a language ideally suited for server-side development. Businesses in particular have been quick to recognize Java s potential on the server Java is inherently suited for large client/server applications.

The crossplatform nature of Java is extremely useful for organizations that have a heterogeneous collection of servers running various flavors of the Unix and Windows operating systems.

Java s modern, object oriented, memory-protected design allows developers to cut development cycles and increase reliability. In addition, Java s built-in support for networking and enterprise APIs provides access to legacy data, easing the transition from older client/server systems.

Servlets make use of the Java standard extension classes in the packages javax.servlet (the basic Servlet framework) and javax.servlet.http (extensions of the Servlet framework for Servlets that are answered with the HTTP requests).

Since Servlets are written in the high-portable Java language and have a standard framework, they provide a means to create sophisticated server extensions in a server and operating system independent way.

Common use for HTTP Servlets

Processing and/or storing data submitted by an HTML form.

Providing dynamic contents, e.g. returning the results of a database query to the client's html.

Managing state of information on top of the stateless HTTP, e.g. for an online shopping cart system which is managing shopping carts for many concurrent customers and maping every request to the right customer.

Traditional servers often use CGI which is treated as an executable which handles the code on the standard shell, and returns html code to the webserver.

Servlets have several differences against CGI applications.

A Servlet is not running in a separate process, thus remove the overhead of creating a new process for each request.

A Servlet is in memory between requests, against CGI program (and probably also an extensive runtime system or interpreter) that needs to be loaded and started for every CGI request.

There is only a single instance which answers all requests concurrently and thus saves memory and allows a Servlet to easily manage high persistent data.

A Servlet could be ran by a Servlet Engines in a restrictive Sandbox which allows secure use and misabuse of untrusted and potentially harmful Servlets.

A Servlet, overall is an part of a class which implements the javax.servlet.Servlet interface.

Although most Servlets extend one of the standard implementations of that interface, mostly javax.servlet.GenericServlet and javax.servlet.http.HttpServlet. In this tutorial we'll be showing only HTTP Servlets which use javax.servlet.http.HttpServlet class.

To start a Servlet, a server application loads the Servlet class (and also the other classes which are addressed by the Servlet) and creates an instance by calling the no-args constructor.

Then it calls the Servlet's init(ServletConfig config) methods. The Servlet should performe one-time setup procedures in these methods and store the ServletConfig object so that it could be later retrieved by addressing the Servlet's getServletConfig() method.

This is managed by GenericServlet. Servlets which extend GenericServlet (or the subclass HttpServlet) should call to super.init(config) at the start of the init method to use of this feature.

The ServletConfig object contains Servlet parameters and a reference to the Servlet's ServletContext. The init method is guaranteed to be called just once during the Servlet's lifecycle. Servlet doesn't need to be thread-safe because the service method will not be called until the call to init returns.

When the Servlet is initialized, its service(ServletRequest req, ServletResponse res) method is addressed for every request made to the Servlet. The method is called concurrently (i.e. multiple threads may call this method at the same time) so it would be implemented in a thread-safe manners.

When the Servlet should be unloaded from the memory (e.g. because a new version should be loaded or the server is shutting down) the destroy() method is addressed. There may still be threads that execute the service method when destroy is called, so destroy is thread-safe.

All resources which were allocated in init should be released in destroy. This method is guaranteed to be called only once during the lifecycle of the Servlet's.

Contact sales!