Overview
The Java Servlet API is a low-level API to extend a web server to generate dynamic content. The API's core is the Servlet interface. Classes implementing this interface can receive the client's HTTP request and generate the response.
Practically all Java web frameworks are built on the Servlet API. This makes it important to know the basics even when you will never write a servlet.
A collection of Servlets, Filters, JSPs and static web content that is bundled together in a single directory is called a web application. Web application are usually distributed and deployed in WAR archives. A web server that supports Java web applications is also called web container.
The Servlet API has two layers, a generic layer and a HTTP-specific layer. The generic part can also be used for other protocols such as FTP, SIP and SMTP. Non-HTTP servlets are not described in this document.
When should you write a Servlet?
Practically all Java Web APIs are built on top of the Servlet API. Whenever you write a web application for Java, you need at least basic knowledge of the API. However, it is rarely necessary to implement a Servlet directly.
Write a Servlet when...
- you serve document types not supported by frameworks, like images and other binaries
- you need full control over the response, for example to use your own web framework
- you need to work on the HTTP level, for example for writing a proxy.
Alternatives
Here are the most common alternatives for pure servlet development. All of them are based on the Servlet API:
- JSP are a powerful, Java-centric template system that also allows you to mix HTML with Java code (more). Today they are mostly used as a template system for higher-level web application frameworks, but for simple applications JSPs without additional framework can be the fastes solution.
- JSF is Java EE's high-level web application framework, designed for complex web applications. It is based on the MVC pattern, offers web components and multiple output formats.
- Spring is a popular alternative. It is a complete application framework that also includes support for web applications, among other features. Typically it is used as a relatively light-weight alternative to EJB application servers.
This list is far from complete, and there is an almost endless number of web frameworks for Java. A few additional, notable frameworks: Struts, Tapestry, Wicket and GWT.
What do you need for Servlets?
In order to run applications written for the Java Servlet API you need a Servlet-capable web container. The most popular choice is Apache Tomcat, but there are many others. Also, all EJB application server ship with a web container.
You web container should include the all JARs that you need to compile your web application (usually a JAR called servlet-api.jar or javax.servlet.jar).
Which container supports which Servlet / JEE version?
| Servlet 3.0 | Servlet 2.5 | Servlet 2.4 | |
|---|---|---|---|
| JEE Version | Java EE 6 | Java EE 5 | J2EE 1.3/1.4 |
| Released | December 2009 | September 2005 | November 2003 |
| in Tomcat | >= 7.0 (*) | >= 6.0 | >= 5.5 |
| in Jetty | >= 8.0 (*) | >= 6.0 | >= 5.0 |
| in Glassfish | >= 3.0 | >= 1.0 | >= 1.0 |
| in JBoss AS | >= 6.0 (*) | >= 4.2 | >= 4.0 |
| in Resin | >= 4.0 | >= 3.1 | >= 3.0 |
| in WAS | - | >= 7.0 | >= 6.0 |
(*) - not stable yet
Short HTTP Introduction
HTTP is a relatively simple, text-based protocol:
- HTTP uses a request/response pattern:
- the client, usually a web browser, sends requests to the web server.
- The server answers with a response.
- The web server can not send anything to the client without the client initiating the connection and requesting it explicitly.
- Every request consists of a command, a path to the requested resource, a set of headers and an optional body
- The only important commands are GET, POST and HEAD:
- GET retrieves a document. Web browsers use it when you visit a page.
- POST sends data, and also gets a document as response. Web browser use POST when you submit a form.
- HEAD simulates a GET without a body in the response. This is important sometimes. For example it allows the browser to find out about a large document before downloading it.
- The path identifies the resource on the server. For example, if you enter the URL "http://jarfiller.com/guide/jaxb/", the requested path is "/guide/jaxb/".
- Headers contain additional information about the request. Header values are just string key/value pairs. Usually the web server evaluates them automatically for you. Even though you can access them directly, the Servlet API provides convenience methods to handle important headers (such as cookies). Wikipedia has a complete list.
- The POST command needs a document as body. For example, if a user submits a form, the content of the fields will be sent in the body. GET requests do not have a body.
- A request may contain parameters:
- GET and most other commands support parameters appended to the path, separated by a question mark ("?") from the rest.
- POST sends parameters in the body
- parameters are sent as name/value pairs, which looks like this: "param1=value¶m2=value¶m3=value"... (more)
- Example URL with parameters: "http://jarfiller.com/search?query=xml&maxresults=20"
- The only important commands are GET, POST and HEAD:
- A response consists of a status code, a set of headers and a body containing a document.
- The status code is just a number. The most important ones:
- Headers contain additional information such as cookies. Header values are just string key/value pairs. Usually the web container will set them automatically for you, so you don't need to know them. Wikipedia has a complete list.
- The response body is the document sent to the client. In a web browser, this is the document shown to the user.
This is what a simple request looks like:
GET /guide/jaxb/ HTTP/1.1 Host: jarfiller.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.8,de-de;q=0.5,de;q=0.3 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive
And here is the server's response (the body containing HTML has been omitted in this example):
HTTP/1.1 200 OK Content-Length: 4227 Date: Mon, 01 Mar 2010 18:47:48 GMT Content-Type: application/xhtml+xml; q=0.8 Server: Apache/2.2.3 (Debian) Last-Modified: Fri, 26 Feb 2010 19:28:08 GMT Accept-Ranges: bytes
An important HTTP concept is the content type, also known as MIME type. It describes the type of document transmitted as body. Basically it is like a file extension, but it can also be used for documents that your servlet created on the fly and thus do not have a file extension. Here are the most important content types:
| Content Type | File Extension | Name |
|---|---|---|
| text/html | .html, .htm | HTML |
| application/xhtml+xml | .xhtml | XHTML |
| text/plain | .txt | Plain text file |
| application/pdf | ||
| image/png | .png | PNG images |
| image/jpeg | .jpg, .jpeg | JPEG images |
| image/gif | .gif | GIF images |
Find a more types on Wikipedia.

