Chapter 15. Web Applications and Web Services

We’re now 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 from a regular Java program is that much of the code, logic, or data resides on the server, at least initally, and the user utilizes a web browser or a lightweight client to access it. This is a very appealing model of software deployment facilitated by the increased standardization and power of HTML and JavaScript in web browsers as well as higher-speed Internet connectivity and better application-to-application web service standards.

Most of this chapter is about the mechanics of the Servlet API, which is a Java framework for writing application components for servers. The Servlet API is used in both Java web applications and often in the implementation of application-to-application web services. We’ll deal with servlets directly in the first part of this chapter, when writing examples used from a web browser. Later, we’ll look at application-level web services that are designed to provide data and services to all types of client applications in a more behind-the-scenes fashion. The two types of server-side applications have some things in common, including how they can be deployed to an application server using a Web Archive (WAR) file and the fact that they are often combined in advanced applications that both render pages on the server and use JavaScript to pull data from web services on the client side.

The Servlet API lives in the javax.servlet package, which is a standard Java API extension. Deploying and running servlets requires an application server or servlet container—a Java-based server that acts like a web server and handles requests bound for servlet components—and so the Servlet API is not bundled with the standard edition of Java. We will recommend that you download Apache Tomcat to run the examples in this chapter and at that time, you can grab the Servlet API JAR file from that distribution in order to compile the example classes. Many Java IDEs can also install the necessary JAR file for you automatically.

The APIs used for building and deploying application-to-application web services are part of the javax.jws package. Although the JWS API is also technically a standard extension, it is bundled with the standard edition of Java and so you can write Java web service clients out-of-the-box, with no additional components. You can even deploy web services directly using a minimal built-in server functionality bundled with the standard edition of Java with no additional application server required. However, this feature is mostly useful for testing, as the built-in server does not perform as well as the various other application servers such as Tomcat. This chapter covers Java Servlet API 3.0 and JWS (JAX-WS) version 2.2.

Servers that support the full set of Java Enterprise APIs including servlets, web services, JSPs, and older technology like Enterprise JavaBeans are called application servers. JBoss is a free, open source Java application server, and BEA’s WebLogic is a popular commercial application server. The free Apache Tomcat server that we’ll use in this chapter started out primarily as a servlet container, but now runs web services and everything needed for serious application development. Tomcat can be used by itself or in conjunction with another web server such as Apache. Tomcat 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/.

Get Learning Java, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.