We're going to take a leap from the client side to the server side to learn how to write web-based Java applications and services. What distinguishes a web-based application or service from a regular Java program is that most of the code and logic reside on the server and the user utilizes a lightweight client or a web browser to access it. This is a very appealing model of software deployment and that is why it is spreading from interactive, browser-based, page-oriented web applications to more formal, application-to-application web services.
Most of this chapter is about the mechanics of the Java Servlet API, which is a framework for writing application components for servers, just as applets are client-side application components for web pages. The Servlet API is used in both Java web applications and in the implementation of web services. However, we'll deal with servlets directly only in the first part of this chapter, when writing web applications to be used from a web browser. Later, when we look at application-level web services, we'll be working with tools that shield us from this level of detail. Nonetheless, the two types of server-side applications still have things in common, including how they are deployed to the server using a Web Archive (WAR) file.
The Servlet API lives in the javax.servlet package, a standard Java API extension, so technically it isn't part of the core Java APIs. Similarly, the APIs used for building and deploying web services are bundled separately in the Java Web Services Developer Pack (JWSDP). In this book, we haven't talked about many standard extension packages, but these are particularly important. This chapter covers Java Servlet API 2.4 and JWSDP Version 1.5. Most web servers support the Java Servlet API either directly or indirectly through add-on modules. Most application servers now also offer a way to deploy web services, but this is not yet as standardized as servlets and web application deployment.
Servers that support the full set of Java Enterprise APIs (including servlets, JSPs, Enterprise JavaBeans, and usually web services) are called application servers. JBoss is a free, open source Java application server available from http://www.jboss.org, and BEA's WebLogic is a popular commercial application server. When we talk about highly modular application components, we often refer to their environment as a container. When we want to talk specifically about the part of these servers that runs servlets or web services, we can refer to them as the servlet container or web services container. We may also generically refer to them as servlet tools, servlet engines, or servlet runners.
We will try to avoid talking in too much detail about particular server environments, but we will use the Apache Project's Tomcat server for the examples in this book. Tomcat is a popular, free servlet and web application container that can be used by itself or in conjunction with other popular web servers. It is easy to configure and is a pure Java application, so you can use it on any platform that has a Java VM. You can download it from http://jakarta.apache.org/tomcat/. Tomcat has been adopted by Sun as part of the J2EE reference implementation, so it always has an up-to-date implementation of the specifications available in both source and binary form. Tomcat Version 5.x is required to support Java Servlet API Version 2.4, but, for our discussion, little else has changed from earlier versions of the server. For our web services example, we'll have to get the reference implementation of Tomcat from Sun, as this works best with the JWSDP.
Except where noted, all of our servlet examples will work equally well with the older Tomcat 4.x and the Servlet 2.3 API. The Servlet APIs and Java documentation can be downloaded directly from http://java.sun.com/products/servlet/. You might consider taking a look at the Java servlet specification white paper, also available at that location. It is a fairly readable specification document.