Title: Java Client Server Computing
1JavaClient Server Computing
2C/S Computing on the Web
Client
Server
HTTP Server
Server API
Form
Post/Get
Applet
RMI
Servlets
JDBC/ODBC
CGI
Direct Socket Operations
DatabaseServer
ApplicationServer
3Client-Server Programming in Java
- Programming network sockets
- Invoking distributed objects using Remote Method
Invocation (RMI) - Communicating with HTTP server
- Accessing remote databases using JDBC
- CGI programming
4Programming Network Sockets
- A TCP/IP Socket is the Java object that connects
a client to a network server that is listening on
a numbered network port for a connection. - Sockets are used to implement reliable
bi-directional point-to-point, stream-based
connections between clients and servers on the
Internet.
5Programming Network Sockets
- Client-side
- (1) Create a Socket object
- InetAddress address
- InetAddress.getByName(IP Address)
- Socket client new Socket(address,
- portNumber)
6Programming Network Sockets
- (2) Create an output stream that can be used to
send info to the Socket. - PrintStream out new
- PrintStream(client.getOutputStream()
- (3) Create an input stream to read the response
from the server. - DataInputStream in new
- DataInputStream(client.getInputStream()
7Programming Network Sockets
- (4) Process input and output with
handleConnection() method and other I/O methods. - ...
- (5) Close the socket when done.
- client.close()
8Programming Network Sockets
- Server-side
- (1) Create a SocketServer object
- ServerSocket listenSocket new
- ServerSocket(portName)
- (2) Create a Socket object (multiple
connectionspossible) from the ServerSocket - Socket server listenSocket.accept()
9Programming Network Sockets
- (3) Create an input stream to read input from the
client - DataInputStream in new
- DataInputStream(server.getInputStream)
- (4) Create an output stream that can be used to
send info back to the client - PrintStream out new
- PrintStream(server.getOutputStream())
10Programming Network Sockets
- (5) Process input and output with
handleConnection() method and other I/O methods. - ...
- (6) Close the socket when done.
- server.close()
11Remote Method Invocation
- RMI (Remote Method Invocation) is Javas
implementation of CORBA (Common Object Request
Broker Architecture). - RMI is an object-oriented version of RPC (Remote
Procedure Call).
12Remote Method Invocation
- The client requests an object from the server
using a simple high-level language. Once it has
the object, it invokes its methods as though it
were a normal local object. (Behind the scene the
requests for the method are rerouted to the
server.) - Neither the client nor the server have to do
anything explicitly with input/output streams or
client/server sockets.
13Remote Method Invocation
- Why Socket not RMI?
- (1) RMI only works among Java systems, not HTTP.
- (2) RMI requires common code to be installed on
both client and server. - (3) RMI requires Java VM to be run at both end.
14Communicating with HTTP Server
- A HTML page can gather user input in two ways
(1) FORMs and (2) Java applets. Both can use
HTTP GET and POST methods for server interaction. - Java supports a much richer interface than do
HTML forms, but building interfaces in Java is
often more difficult than in HTML.
15Using Java applets to send GET data to CGI
programs
- (1) The showDocument() method can be used to
request the browser to display a particular URL. - The URLEncoder.encode method can be used to code
the string being send into HTTP readable form. - (SearchService.java)
16Sending Data via GET and Processing the Results
- If you are developing both the client and the
server end of the process, you can then have the
server CGI return data in non-HTML format and
have the client CGI process the data and present
it in graph or some other custom format. - (ShowFile.java)
17Using Java Applets to Send POST Data to CGI
Programs
- If you are developing both the client and the
server end of the process, you can then have the
client CGI send POST data to the server CGI.
18Java Database Connectivity
- JDBC is another API provided by Java for database
connectivity. - There are 7 steps to query database
- (1) Load the JDBC driver.
- String driver
- (connect.microsoft.microsoftDriver)
- Class.forName(driver)
19Java Database Connectivity
- (2) Define the connection URL
- String url jdbcff-sybase//host/database)
- (3) Establish the connection
- String user username
- String password password
- Connection connection
- DriverManager.getConnection(url, user,
- password)
20Java Database Connectivity
- (4) Create a Statement intance for query.
- Statement statement
- connection.createStatement()
- (5) Execute a statement.
- String query SELECT FROM table
- ResultSet results
- statement.executeQuery(query)
- (6) Process the resutls.
- (7) Close the connection.
21Common Gateway Interface
- The CGI lets HTML pages communicate with database
or other programs on a system running an HTTP
server. - Instead of treating a CGI program as a file whose
content should be returned, the HTTP server
treats it as a program whose output should be
returned.
22Common Gateway Interface
- So, a CGI program processes data from the client
and return the results. - A CGI program should perform 4 steps
- (1) Read the data.
- (2) Output HTTP headers.
- (3) Send a blank line.
- (4) Generate a HTML document.