In this chapter the concept of Servlets, not the entire Servlet specification, is explained; consider this an introduction to the Servlet specification starting strictly with Servlets. At times the content of this chapter may seem dry, even reminiscent of the actual specification. While an attempt is always made to liven the material up, however, there are several relevant but boring aspects of Servlet development that need to be presented now. Do attempt to read the whole chapter straight through, but also remember you can always reference this chapter when needed.
This chapter discusses the following topics:
An explanation of what Servlets are and why you would want to use them.
The Servlet life cyclethat is, how a container manages a Servlet.
Building Servlets for use on the World Wide Web, which includes a review of the HTTP protocol.
Configuring Servlets using web.xml.
Coding both text-producing and non-text-producing Servlets.
Handling HTML forms and file uploads.
Request dispatchingServlet to Servlet communication and including or forwarding to other resources in the Web Application.
Application context and communicating with the container via a Servlet.
Servlet event listeners.
What Servlets Are and Why You Would Want to Use Them
Java Servlets are an efficient and powerful solution for creating dynamic content for the Web. Over the past few years Servlets have become the fundamental building block of mainstream server-side Java. The power behind Servlets comes from the use of Java as a platform and from interaction with a Servlet container. The Java platform provides a Servlet developer with a robust API, object-orientated programming, platform neutrality, strict types, garbage collection, and all the security features of the JVM. Complimenting this, a Servlet container provides life cycle management, a single process to share and manage application-wide resources, and interaction with a Web server. Together this functionality makes Servlets a desirable technology for server-side Java developers.
Java Servlets is currently in version 2.4 and a part of the Java 2 Enterprise Edition (J2EE). Downloads of the J2SE do not include the Servlet API, but the official Servlet API can be found on Sun Microsystems' Servlet product page, http://java.sun.com/products/servlets, or bundled with the Java 2 Enterprise Edition. Servlet API development is done through the Java Community Process, http://www.jcp.org, but the official reference implementation of the Servlet API is open source and available for public access through the Tomcat project, http://jakarta.apache.org/tomcat.
The Servlet 2.4 API includes many features that are officially defined by the Servlet 2.4 specification, http://java.sun.com/products/servlets, and can be broken down as follows.
Servlets are always part of a larger project called a Web Application. A Web Application is a complete collection of resources for a Web site. Nothing stops a Web Application from consisting of zero, one, or multiple Servlets, but a Servlet container manages Servlets on a per Web Application basis. Web Applications and the configuration files for them are specified by the Servlet specification.
Servlets and HTTP Servlets
The primary purpose of the Servlet specification is to define a robust mechanism for sending content to a client as defined by the Client/Server model. Servlets are most popularly used for generating dynamic content on the Web and have native support for HTTP.
Filters were officially introduced in the Servlet 2.3 specification. A filter provides an abstracted method of manipulating a client's request and/or response before it actually reaches the endpoint of the request. Filters greatly complement Servlets and are commonly used for things such as authentication, content compression, and logging.
Servlets already use the security features provided by the Java Virtual Machine, but the Servlet specification also defines a mechanism for controlling access to resources in a Web Application.
One of the best features of a Servlet is the ability to develop content for just about any language. A large part of this functionality comes directly from the Java platform's support for internationalization and localization. The Servlet API keeps this functionality and can be easily used to create content in most of the existing languages.
The focus of this chapter is to introduce Servlets and explain how to use HTTP Servlets for creating dynamic content on the Web. For simplicity, this chapter focuses on the basics of Servlets and leaves more complex but practical examples for discussion in pertinent, later chapters. Filters, security, and true internationalization issues are all discussed in later chapters as they pertain to both Servlets and JSP.