Web apps and Web Services

We're going to take a leap from the client side to the server side to learn how to write web-based Java apps and services. What distinguishes a web-based app 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 apps to more formal, app-to-app web services. Most of this chapter is about the mechanics of the Java Servlet API, which is a framework for writing app components for servers, just as applets are client-side app components for web pages. The Servlet API is used in both Java web apps 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 apps to be used from a web browser. Later, when we look at app-level web services, we'll be working with tools that shield us from this level of detail. Nonetheless, the two types of server-side apps 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 tutorial, 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 app servers now also offer a way to deploy web services, but this is not yet as standardized as servlets and web app deployment. Servers that support the full set of Java Enterprise APIs (including servlets, JSPs, Enterprise JavaBeans, and usually web services) are called app servers. JBoss is a free, open source Java app server available from http://www.jboss.org, and BEA's WebLogic is a popular commercial app server. When we talk about highly modular app 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 tutorial. Tomcat is a popular, free servlet and web app 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 app, 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.oracle.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.

Java ScreenShot