Servlets: Bonus for Java™ Developers
There is much excitement over the Internet and the World Wide Web. The Internet ties the “information world” together. The World Wide Web makes the Internet easy to use and gives it the flair and sizzle of multimedia. Organizations see the Internet and the Web as crucial to their information systems strategies. Java provides a number of built-in network-ing capabilities that make it easy to develop Internet-based and Web-based applications. Not only can Java specify parallelism through multithreading, but it can enable programs to search the world for information and to collaborate with programs running on other com-puters internationally, nationally or just within an organization. Java can even enable ap-plets and applications running on the same computer to communicate with one another, subject to security constraints.
Networking is a massive and complex topic. Computer science and computer engi-neering students typically take a full-semester, upper-level course in computer networking and continue with further study at the graduate level. Java provides a rich complement of networking capabilities and will likely be used as an implementation vehicle in computer networking courses. In Advanced Java 2 Platform How to Program we introduce several Java networking concepts and capabilities.
Java’s networking capabilities are grouped into several packages. The fundamental networking capabilities are defined by classes and interfaces of package java.net, through which Java offers socket-based communications that enable applications to view networking as streams of data—a program can read from a socket or write to a socket as simply as reading from a file or writing to a file. The classes and interfaces of package java.net also offer packet-based communications that enable individual packets of information to be transmitted—commonly used to transmit audio and video over the Internet. Our book Java How to Program, Fourth Edition shows how to create and manip-ulate sockets and how to communicate with packets of data.
Higher-level views of networking2 are provided by classes and interfaces in the java.rmi packages (five packages) for Remote Method Invocation (RMI) and org.omg packages (seven packages) for Common Object Request Broker Architecture (CORBA) that are part of the Java 2 API. The RMI packages allow Java objects running on separate Java Virtual Machines (normally on separate computers) to communicate via remote method calls. Such method calls appear to be to an object in the same program, but actually have built-in networking (based on the capabilities of package java.net) that communicates the method calls to another object on a separate computer. The CORBA packages provide similar functionality to the RMI packages. A key difference between RMI and CORBA is that RMI can only be used between Java objects, whereas CORBA can be used between any two applications that understand CORBA—including applications written in other pro-gramming languages.
Our discussion of networking over the next two chapters focuses on both sides of a client-server relationship. The client requests that some action be performed and the server performs the action and responds to the client. This request-response model of communica-tion is the foundation for the highest-level views of networking in Java—servlets and Java-Server Pages (JSP). A servlet extends the functionality of a server. Packages javax.servlet and javax.servlet.http provide the classes and interfaces to define servlets. Packages javax.servlet.jsp and javax.servlet.jsp.tagext provide the classes and interfaces that extend the servlet capabilities for JavaServer Pages. Using special syntax, JSP allows Web-page implementors to create pages that use encapsu-lated Java functionality and even to write scriptlets of actual Java code directly in the page.
A common implementation of the request-response model is between World Wide Web browsers and World Wide Web servers. When a user selects a Web site to browse through their browser (the client application), a request is sent to the appropriate Web server (the server application). The server normally responds to the client by sending the appropriate XHTML Web page. Servlets are effective for developing Web-based solutions that help provide secure access to a Web site, interact with databases on behalf of a client, dynamically generate custom XHTML documents to be displayed by browsers and main-tain unique session information for each client.
This chapter begins our networking discussions with servlets that enhance the func-tionality of World Wide Web servers—the most common form of servlet today. Chapter 31 discusses JSPs, which are translated into servlets. JSPs are a convenient and powerful way to implement the request/response mechanism of the Web without getting into the lower-level details of servlets. Together, servlets and JSPs form the Web tier of the Java 2 Enter-prise Edition (J2EE).
Many developers feel that servlets are the right solution for database-intensive appli-cations that communicate with so-called thin clients—applications that require minimal client-side support. The server is responsible for database access. Clients connect to the server using standard protocols available on most client platforms. Thus, the presentation-logic code for generating dynamic content can be written once and reside on the server for access by clients, to allow programmers to create efficient thin clients.
Sun Microsystems, through the Java Community Process, is responsible for the develop-ment of the servlet and JavaServer Pages specifications. The reference implementation of both these standards is under development by the Apache Software Foundation
(www.apache.org) as part of the Jakarta Project (jakarta.apache.org). As stated on the Jakarta Project’s home page, “The goal of the Jakarta Project is to provide commercial-quality server solutions based on the Java Platform that are developed in an open and coop-erative fashion.” There are many subprojects under the Jakarta project to help commercial server-side developers. The servlet and JSP part of the Jakarta Project is called Tomcat. This is the official reference implementation of the JSP and servlet standards. We use Tomcat to demonstrate the servlets in this chapter. The most recent implementation of Tomcat at the time of this writing was version 3.2.3. For your convenience, Tomcat 3.2.3 is included on the CD that accompanies Advanced Java 2 Platform How to Program. However, the most recent version always can be downloaded from the Apache Group’s Web site. To execute the serv-lets in this chapter, you must install Tomcat or an equivalent servlet and JavaServer Pages implementation. We discuss the set up and configuration of Tomcat in Section 30.3.1 and Section 30.3.2 after we introduce our first example.
In our directions for testing each of the examples in this chapter, we indicate that you should copy files into specific Tomcat directories. All the example files for this chapter are located on the CD that accompanies this book and on our Web site www.deitel.com.
[Note: At the end of Section 30.10, we provide a list of Internet specifications (as dis-cussed in the Servlet 2.2 Specification) for technologies related to servlet development. Each is listed with its RFC (Request for Comments) number. We provide the URL of a Web site that allows you to locate each specification for your review.]
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.