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.
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 |
| Figure B |
![]() |
- 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 java.io.PrintWriter to inject the response data into the HTTP stream.
Tech Update forum. Find out what's where in the new Tech Update with our
Guided Tour. Let the editors know what you think in the
Mailroom.







