Java servlets offer a simple way to extend and enhance Web server functionality. They’re platform-independent, so you can select the best method for your particular servers or platform. In addition, servlets use the entire Java repertoire of APIs. Our focus here will be on how the servlet classes that the Java platform provides enable you to build Web applications using the same object-oriented techniques used in a fat client environment.

Transitioning to object-oriented programming (OOP)

Our previous installment covered the Java tools you need in order to access and use valuable Internet resources in your applications. See how to work with Internet content via the URL class in the article “Using Java to access Web resources.”

Introducing servlets
The Java servlet architecture facilitates server-side processing using the Java programming language. Since servlets are written in Java, they empower server-side developers with all of Java’s benefits and APIs, including the ability to write a servlet once and run it on any Java-enabled server platform.

Servlets offer a major leap forward from traditional CGI development. Each servlet request is handled by a thread instead of an entirely new process, as is the case with traditional CGI implementations. Servlets are developed using the Java Servlet API that is packaged in the namespaces javax.servlet and javax.servlet.http. Within these two packages are classes, interfaces, and frameworks that encapsulate functionality and properties for all of the HTTP protocol’s request/response communications, including session handling, POST requests, and GET requests.

Since servlets are written using the Java programming language, all the facilities that a Java class enjoys are also available to the servlet environment. Certainly one of the most important and useful of these is the ability to access a SQL database using the Java Database Connectivity (JDBC) API.

Servlet architecture
The featured component created with the servlet API is the servlet itself. A servlet is an object that extends either the javax.servlet.GenericServlet class or the javax.servlet.http.HttpServlet class. The javax.servlet.GenericServlet class defines methods for building generic, protocol-independent servlets. The javax.servlet.http.HttpServlet class extends this class to provide HTTP-specific methods. The diagram in Figure A illustrates the hierarchy of a typical HTTP servlet and the relationships and functionality exposed by the classes in the hierarchy.

Figure A
Class hierarchy

As you can see, the GenericServlet class defines generic server-side operations; the HttpServlet extends this to define HTTP-specific operations; and application-specific servlets extend this to provide application-specific operations. We will be referring to servlets created for the HTTP environment throughout this article.

Client-servlet interaction
In a typical HTTP servlet environment, a user instigates a session with a servlet by directing a Web browser at a URL that loads a servlet on a server. The browser then communicates a request to the servlet over a socket on port 80. The servlet, listening on port 80, fields the request, extracts any parameters or data from the request, and dispatches the application flow to the appropriate business-logic component to execute one or more tasks. Once the business-logic component has finished executing, it passes its results back to the servlet to be formatted as HTML and passed back to the user’s browser.

Figure B illustrates the typical interaction between a browser, a servlet, and a business-logic component.

Figure B

Servlet example
Listing A provides the code for a simple HTTP servlet. In our example, I will assume that all requests are HTTP GET requests. The GenericServlet and the HttpServlet both expose a method called service, which can be used as the point to intercept the request from a client. The HttpServlet extends this by diagnosing the HTTP request and dispatching it to HTTP-specific methods, such as doPost and doGet. Since all requests are HTTP GET requests, I need to focus only on the doGet method.

Here are some points to notice in this example:

  • ·        SimpleServlet extends the javax.servlet.http.HttpServlet class.
  • ·        SimpleServlet overrides the doGet method in the javax.servlet.http.HttpServlet class to field the HTTP request, dispatch the business logic, and formulate the response.
  • ·        The HTTP request is encapsulated in an object of type javax.servlet.http.HttpServletRequest.
  • ·        The HTTP response is encapsulated in an object of type javax.servlet.http.HttpServletResponse.
  • ·        We can use a standard to inject the response data into the HTTP stream.

Using the Java servlet API, you can enjoy all the benefits afforded by the programming language and carry these benefits into the traditional CGI environment. The classes provided by the servlet API allow us to interact with HTTP-enabled applications using most of the same techniques and experiences learned from programming in a desktop environment.

Our next article will explore more of the objects available to us from the servlet API, which we can use to make server-side application development a relatively painless endeavor.