Lemur zaprasza
Red Hat® Linux 6 Unleashed
Chapter 35: Java Programming Previous
Sections in this Chapter: What Is Java? An Overview of the Java Language Java Support in Red Hat Linux Writing Java Programs Other References Software Development Kits (Java Development Kits) Java and the Internet Previous SectionNext Section Java and the Internet java.net: Networking Techniques Java and the Internet complement each other very well. The Internet launched Java into the mainstream and made the benefit of distributed cross-platform applications a necessity. Since Java's introduction, several new Java APIs have been developed to help bring the Internet and enterprise computing closer to Java. These APIs are discussed next. JDBC JDBC is an acronym for Java Database Connectivity. The JDBC API provides a generic high-level interface for database connectivity to a DBMS (DataBase Management System). The JDBC class library is released as part of the JDK in a package called java.sql. JDBC is implemented in two layers:, the JDBC API database abstraction layer and the driver layer. The JDBC layer consists of the JDBC DriverManager and classes provided to connect to a particular database. The driver layer contains a database-specific driver that knows how to communicate with the database. With this abstraction, when the database changes, the driver can be switched with very few changes to the Java code.The JDBC interfaces in the java.sql package are listed here: CallableStatement Connection DatabaseMetaData Driver PreparedStatement ResultSet ResultMetaData Statement JDBC classes in the java.sql package are listed here: Date DriverManager DriverPropertyInfo Time Timestamp Types The most important JDBC class is DriverManager. This class is used to load the appropriate driver for the DBMS that you are using for your application. DriverManager also provides a basic service for managing JDBC drivers.JDBC drivers can be split into four categories: JDBC-ODBC, Native API, Java-Net, and Native protocol. The Native protocol drivers are best suited for Internet use because they do not rely on a DBMS client. Most major DBMSs have support for 100 percent Java driver implementations. RMI RMI is an acronym for Remote Method Invocation. RMI can be thought of as similar to RPC (Remote Procedure Call). RPC is a convention for C programmers to remotely call procedures over the network using a skeleton/stub approach. RMI also uses a skeleton/stub approach for distributed applications to communicate with one another. That is where the similarities stop. RMI also introduces the idea of remote objects and not just remote procedures. Many developers are using CORBA (Common Object Request Broker) to build distributed systems in large heterogeneous environments. CORBA is a specification held by a consortium called the Object Management Group. This consortium exists to fill the need for interoperability among the constantly increasing number of hardware and software products available today. CORBA defines an IDL (Interfaces Definition Language) and APIs that enable client/server object interaction within a specific implementation of an Object Request Broker (ORB). More info on CORBA can be found at . CORBA is platform- and language-independent. The IDL is translated into a specific language for the chosen implementation. There even exist several implementations for the Java platform. Why use RMI? Note - There is work being done to implement RMI using the OMG's IIOP (Internet Inter-ORB Protocol). More information can be found at the Java Developer Connection at . RMI has some advantages over CORBA. RMI does not require an IDL but instead is only implemented for the Java language. This can be a plus if you are dealing with only Java in your distributed system. RMI also utilizes Java object serialization to marshal and unmarshal parameters, which is relatively lightweight on the network. Marshalling is the process of converting an object to a format that can be sent over the network.RMI has some added security features. RMI uses the security manager, Java's convention for security policies, defined to protect systems from hostile applets to protect your systems and network from potentially hostile downloaded code.Java RMI is included with the Java Base API under the java.rmi package. Table 35.9 lists of some of the most important ones. TABLE 35.9 Import RMI Classes Class Description Remote Remote is an interface defined in the java.rmi package. The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Naming Naming is a bootstrap mechanism, a class located in java.rmi for obtaining references to remote objects based on Uniform Resource Locator (URL) syntax. RemoteServer RemoteServer, located in java.rmi.server, is a superclass for all server implementations and provides the interface for functions needed to create and export remote objects. Note - More information on RMI including sample code and tutorials can be found on the JavaSoft Web site at . The JDK also includes a couple of tools for RMI, rmic, and the rmiregistry. The rmic compiler generates stub and skeleton class files for remote objects from the names of compiled Java classes that contain remote object implementations. (A remote object is one that implements the interface java.rmi.Remote.)rmiregistry is a tool that creates a remote object registry that is a naming service used by RMI servers on a host to bind remote objects to names. java.net: Networking Techniques A language that touts itself as network-savvy requires a good network library. This is why the Java Base API contains the java.net package. The java.net package contains an extensive library of classes for networking. The Java networking package is based on widely accepted Internet protocols such as TCP/IP and HTTP to specify network resources.Some common networking classes include Socket ServerSocket DatagramSocket InetAddress URL MulticastSocket The two most important classes in this package are Socket and ServerSocket.These two classes can be used to create client/server connections via a TCP/IP streamed connection. Streamed TCP/IP connections work similar to input/output streams. In fact, you need to obtain a stream from the socket class in order to communicate across a socket.The WebSocket Web client example demonstrates how to write a simple Java client using the Socket class to connect to a Web server: import java.net.Socket; import java.net.URL; import java.net.MalformedURLException; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; // WebSocket class will connect to a webserver and receive a webpage // Try it by typing: java WebSocket http://www.mcp.com public class WebSocket { private PrintWriter sockout; private BufferedReader sockin; private Socket sock; public static void main( String args[] ) { // Create our WebSocket class and give a URL object formed from // the command line arg try { new WebSocket( new URL(args[0]) ); } catch( MalformedURLException mfu ) { System.err.println( "Invalid URL format..." ); System.err.println( "Usage: java WebSocket [url]" ); } } public WebSocket( URL webadd ) { try { // Open up a new socket connection to the host on the HTTP port 80 sock = new Socket( webadd.getHost(), 80 ); // Creat the Reader and Writer for the Socket sockout = new PrintWriter( sock.getOutputStream() ); sockin = new BufferedReader( new InputStreamReader(sock.getInputStream()) ); // Tell the webserver what file you want sockout.println( "GET " + webadd.getFile() ); sockout.flush(); // Print out the HTML the webserver returned to the console for( String line = sockin.readLine(); line != null; line = sockin.readLine() ) System.out.println( line ); } catch(IOException ioe) { System.err.println( "Error connecting to host " + webadd.getHost() ); } } } This is a simple Web client application written in Java that makes a socket connection to a Webserver on port 80, the HTTP port where the server listens for requests. After a connection has been made, the WebSocket Web client asks the server to serve the HTML document you entered. This is exactly what your Web browser does. Most Web browsers have complex parsing and graphics engines to format and display the HTML in a more interesting fashion. Red Hat® Linux 6 Unleashed
Chapter 35: Java Programming Previous
Sections in this Chapter: What Is Java? An Overview of the Java Language Java Support in Red Hat Linux Writing Java Programs Other References Software Development Kits (Java Development Kits) Java and the Internet Previous SectionNext Section © Copyright Macmillan USA. All rights reserved. |