HTTP Basics, Web Server, Servlet Container and the Java Servlet API
This article tries to demystify HTTP, "servlet", "web server", "application server", "servlet container" and gives the fundamentals of the Java Servlet API (that comes with the J2EE SDK).
Introduction to HTTP
The ABC of HTTP
HTTP (Hyper Text Transfer Protocol) is one of the numerous protocols, which computers use, to talk to each other (just the same way two human beings need a common language to communicate). When I say 'talk to', I mean, exchanging data. In the client-server world, a client is the machine that makes the first call to the server (thinking of the word "accost", which means 'to approach and to speak first'). A machine can be both client and server.
When a computer initiates a connection to another computer, in HTTP jargon, it is a request. When the server computer sends data back, it is a response. An HTTP server accepts request from any client and always sends a response. Once a response is sent, the server does not retain information about the request. Neither does it retain any information about the client. That is why HTTP is a stateless protocol.
An HTTP request has three parts: (a) a request line, (b) one or more headers and (c) a message. The message is optional.
- (a) Request Line: A request looks like:
GET /science /light.html HTTP/1.1
'GET' is the name of the method. In HTTP 1.0, other methods are: HEAD and POST. In HTTP 1.1, additional methods available are: PUT, OPTIONS, DELETE, TRACE, CONNECT. The second token of the message, "/science/light.html", is a URI. URI stands for Universal Resource Identifier. URI gives information about the location of the resource to be gotten. The last token is the version of HTTP to be used. The current version is 1.1. 'GET' method is used to retrieve a resource identified in the URI. The 'POST' method, on the other hand, is used to send data to the server. 'GET' can also send data to the server, but is limited to 255 characters, and is in the form of name-value pairs, separated by ampersand. An example of sending data to the server using GET is:
GET /science /light.html?user=john&pwd=abcd HTTP/1.1
POST method is used to send HTML FORM values to the server. GET and POST are the common methods used.
(b) Headers:Headers contain meta-information. 'Meta-information' means information about information. They include information like: what is the type of data (character, binary etc.), what is the size of data (in Kilobytes).
(c) Message: Message body is optional. POST has a body with all the information from the HTML FORM. GET does not have one. If a message body exists, it is preceded by a blank line.
A HTTP Response, the same way, has a (a) response line, (b) one or more headers, and (c) a message.
- (a) Response Line: A response line looks like:
HTTP/1.0 200 OKAs you might have guessed, the first token is the HTTP version. The second token is one of the many predefined status codes, and the last one is an English description of the code.
(b) and (c) share the same characteristics as that of as a HTTP Request.
Web Server is a machine that has a HTTPD service running. A web server is the one that handles HTTP requests and generates HTTP responses.
A Servlet is a server-side entity for servicing HTTP requests.
A Servlet Container is a sub-set of a Web Server. A Servlet Container is a separate module; it may run within the web server as a single program (called Standalone, in this case), may run as a different program, but part of the same address space (In-Process), or run in different process-spaces. Tomcat is a very popular standalone servlet container, and can be acquired from http://jakarta.apache.org.
A web application is an application that is accessible from the web. In that sense, all servers working towards the purpose of running a web application is an application server.
Tomcat is an application server, in that sense, if that is the only server used in an application. An application may use other servers, like BEA WebLogic or IBM WebSphere. These application servers provide additional services like an EJB Server, a JMS Server, a JNDI Naming Server, etc.
The world of Java Servlets
Sun provides a set of specifications, which dictate how a servlet would communicate with a web server. Tomcat is a servlet container that conforms to those specifications. So, when application developers use the Servlet API, we are happy campers, because Tomcat has implemented the Java interfaces we are going to use. API stands for Application Programming Interface. It is a set of classes and interfaces for us to use. Servlet API is a part of Sun's J2EE SDK, that can be downloaded from http://java.sun.com/j2ee.
This is the numero uno interface of the entire API. Any Servlet class implements this interface. The five methods to implement are: init(), service(), destroy(), getServletConfig() and getServletInfo()
javax.servlet.GenericServlet (abstract class)
Abstract class, implements the java.servlet.Servlet interface, provides implementation of all methods except the service method.
This interface abstracts a Request.
A generic interface for abstracting a response.
javax.servlet.http.HttpServlet (abstract class)
Extends GenericServlet. Has a new method:
protected void service (HttpServletRequest request,
throws ServletException, java.io.IOException;
This interface extends ServletRequest, and gives HTTP-specific functionality to retrieve information about request headers, request parameters, request attributes.
Extends ServletResponse. Provides abstraction for a HTTP response. Methods to set header types, content types and response body exists.
This is an important interface to understand. It provides a rendezvous for all servlets, JSP's of a web application to share common knowledge. There is one and only ServletContext for an application. Methods to look for are:
java.netURL getResource(String relativePath)
java.io.InputStream getResourceAsStream(String relativePath)
ServletConfig helps us get the values of initialization parameters as declared in the deployment descriptor file. A "deployment descriptor" is a XML document, which can be used to declare initial parameters that the application can use at run-time. The deployment descriptor is named "web.xml". A snippet of web.xml with servlet initialization parameters:
<web-app> <servlet> <servlet-name>HumbleServlet</servlet-name> <servlet-class>com.abcd.HumbleServlet</servlet-class>
Now it is easy to get the parameter value by calling getInitParameter (String paramName), which returns a String. Database driver names, classes, urls's are good candidates for entry.
Servlet container implements this interface. A representation of a session. The container takes care of associating the right session for a user.
Interface to pass ServletRequest, ServletResponse objects between servlets. From a servlet, one can use it to forward control to a different servlet, a JSP, a HTML or any resource using the method:
public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOExceptionFor including contents of a different resource, one can use:
public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException
Accessing a database from a Servlet
It is very easy to access a database from a Servlet. If using JDBC, then one can instantiate a driver class and get hold of a ?connection? object. The driver class and connection interface is a class provided by Sun in its software development kit (SDK, for short). Both J2SE and J2EE come with their driver classes.
Connection to the database can be configured in the deployment descriptor file. Another trick would be to get the connection in the init(..) method. This way, the same connection can be re-used. Care must be taken to use connection objects, because a connection to a database is expensive, and can slow your application down.
If you chose to use a persistence layer instead of JDBC, like Object Relational Bridge from Apache Software Foundation, or iBatis, then you need not worry about writing code to access database. Necessary configuration files (XML documents) take care of them.
Another thing to keep in mind is servlets are not thread-safe (i.e. multiple threads can access the service(...) method.
With clearing some of the roadblocks that a "Servlet" newbie might encounter, I am hoping that after reading this article, she or he might be able to go a little deeper into the subject.
Please e-mail if there is anything you want to see here, in the Java Web technology domain.
Prepared by Saikat Goswami, Boston, Massachussets, firstname.lastname@example.org