Chapter 3. Java Servlets


Over the last few years, Java has become the predominant language for server-side programming. This is due in no small part to the Java Servlet API , which provides a standard way to extend web servers to support dynamic content generation. In terms of enterprise computing, servlets are a natural fit if you are using the Web as your deployment platform. You can take advantage of web browsers as universally available thin clients using the web server as middleware for running application logic. Under this model, the user makes a request of the web server, the server invokes a servlet designed to handle the request, the servlet fulfills the request, and the result is returned to the user for display in the web browser.

While this sounds like every other dynamic content technology (such as CGI, ISAPI, ASP, PHP, and the like), servlets have some major advantages. For one, servlets are persistent between invocations, which dramatically improves performance relative to CGI-style programs. Servlets are also 100% portable across operating systems and servers, unlike any of the alternatives. Finally, servlets have access to all the APIs of the Java platform, so, for example, it is easy to create a servlet that interacts with a database, using the JDBC API (see Chapter 8).

The Servlet API also provides the basis for programming with JavaServer Pages (see Chapter 4), Struts (see Chapter 19), and Java Server Faces (see Chapter 5). In fact, if you use one of these frameworks to structure your application, you may never need to write a servlet at all. But since JSP, JSF, and Struts are all designed to work as components of larger applications, you can mix your own servlets, filters, and session management into your framework-based applications, and that makes a good knowledge of the Servlet API essential for advanced enterprise development.

Version 2.3 of the Servlet API is a required component of J2EE Version 1.3. A small update, Version 2.4, came out in 2003 and is part of the J2EE Version 1.4 specification. Since the two versions are very similar (and there are still a lot of Version 2.3 applications out there), you can assume that anything in this chapter applies equally to both versions unless we specifically say otherwise.

This chapter demonstrates the basic techniques used to write servlets using the Servlet API, including some common web development tasks such as cookie manipulation and session tracking. This chapter assumes that you have some experience with web development; if you are new to web development, you may want to brush up on web basics by consulting Webmaster in a Nutshell by Stephen Spainhour and Robert Eckstein (O'Reilly). For a more complete treatment of servlets, check out Java Servlet Programming by Jason Hunter with William Crawford (O'Reilly).

Other chapters in this book also cover topics relevant to servlet development, and you may want to consult them after digesting the following material on developing servlets. Chapter 2 discusses the general J2EE deployment model for applications and components and goes into detail about deploying web components. Chapter 10 provides an overview of J2EE security concepts and provides details on integrating security features into web applications. Chapter 21 discusses code annotations that can be used in your servlet code to automatically generate deployment descriptors and other artifacts for your web applications, and Appendix A provides a reference for web deployment descriptors.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net