Java Network Programming

Overview of the Java Programming Language

Normal programs:

Microsoft Word for Windows -> PC
Microsoft Word for Macintosh -> Mac

Java programs:

Java Program -> (Java compiler) -> bytecodes ->

Windows Virtual Machine -> PC
Macintosh Virtual Machine -> Mac
UNIX Virtual Machine -> UNIX

Architecture Neutral
Bytecodes are platform independent

Java network programs

Applets (download from server to client, execute on client's browser)
Servlets (execute on server, enhance server's functionality beyond HTTP)


Sockets (allow two programs on two different machines to communicate)
Remote Method Invocation (execute methods on another machine)

Security features

Sandbox model (untrusted code is placed in a sandbox, cannot do harm to the machine)
Digital signature for downloaded Java code for finer control of permissions

Java language features


Object-oriented (single inheritance -- simplicity)
Strongly typed
Garbage collection, no pointers
(simplifies memory management, eliminates memory-related bugs,may add overhead).

Graphics and GUI design support
Multithreading support
(multiple tasks executed "concurrently")
Dynamic (
class libraries can be loaded into a java interepreter at any time)

What is Object Oriented programming?

Physical Object: A car.

A. User (or driver) perspective
Parts: Attributes

Name of Attribute (real world) Type (programming concept)
engine (on/off) binary
throttle (closed....fully open) real
gearbox (neutral, 1st/2nd/3rd/4th gear, reverse) integer
steering wheel (-180...180 degrees) real
wheels (free turning, brake pressed) binary

Operations: Methods

Operation (real world) Method (programming concept)
turn engine on or off  
press gas pedal  
set gearbox to a gear  
turn the steering wheel  
press brake  
look at the speedometer (engine rpm)  
look at the gearbox  

State of an object: The set of values of its attributes
Operation: May change the state

B. Mechanic perspective

Engine:
cylinders spark plugs valves distributor carburettor
Electrical system:
starter motor alternator battery

Information hiding: The user does not need to know as much as a mechanic to drive a car

Class: A template that describes in a general way similar objects.
Object: An instance of a class

Example: Your car, a 1995 Ford Escort, is an object, i.e. an instance of class "car".

In contrast to OOP: Procedural abstraction (the old style)

Program is a recipe:

To cook lasagna:

cook pasta
cook ground meat
prepare sauce
layer ground meat, sauce, cheece between pasta

To cook pasta

put water in a pot
bring water to boil
boil lasagna for 10 minutes
drain

To cook ground meat

put ground meat in the pot
add 1/2 cup water
bring to boil
lower heat
simmer for 15 minutes

To prepare sauce

.....

Procedural abstraction suitable when data is simple but operations are complex (e.g. numerical processing)

Object-oriented programming suitable when data is complex (e.g. bank information systems, airline ticketing, warehouse inventory management, customer records).

Object-oriented programming generalizes procedural abstraction.

What is the oldest object-oriented programming language?

Hint: C_ _ _ _

Java Network Programming


General property: Java standardizes and abstracts network language facilities. It makes it simpler than before for the general programmer to write networked programs.


URL class:
protocol, host name, port, path, filename

URLConnection: open connection to a specified network resource (URL)

Socket: Treat a network connection as an open file (for reading/writing)

(operations for both clients and servers)
Connect to remote machine (prepare to send/receive data)
Send data
Receive Data
Close connection
(operations for servers only)
Bind to a port
Listen for incoming data
Accept connections from remote matchines on bound port

Remote method invocation (RMI): Java objects on different hosts can communicate

Remote object lives on a server
Client can invoke the methods of the remote object as if they were local

Security manager may restrict operations the remote method can perform locally
(e.g. reading or writing client files)

Most general solution is CORBA (Common Object Request Broker Architecture), which allows the above for objects written in different programming languages.


Servlets

Need for providing interactive experience for the user, instead of just navigating static content.

Examples:

- keyword search in a document archive
- remote database query
- electronic storefront (shopping basket, payment processing)
- on-line game

How:
- web server passes requests to an external program
- output of program is sent to the client instead of a static file

De facto standard is CGI (Common Gateway Interface):
a standard way for an information server to talk with external applications

CGI programs can be in any programming language.
Perl is the predominant one.

Problem: each request must start a separate Perl interpreted (wasteful)


Java Servlets:
Java classes that expand the functionality of the server.

Advantages:
-
servlets run as separate threads within the web server process
- servlets can interact very closely with the server
- can use full power of Java
- type safety of Java
- Java exception handling (more difficult to crash the server)
- Java security manager

Typical uses of servlets

- Handling form data
Get input from the user via an HTTP form
Pass input data to a servlet
Servlet makes web page using form data and ships to client

- Filtering HTML pages
Specify that all html files should be handled by a filtering servlet before sent to the client.

- Generating and sending multimedia content to the client
Image composition
Writing over an image
Image effects

- Session tracking
Use instead of cookies




Java Security

Original Java Security model

Application: full access to computer's resources (file system, network connections)
Applet: no access to computer's file system (sandbox)

New Java Security model

User-defined sandbox

Security of the computer's memory from a malicious piece of code

Classes are given privileges depending on where they come from

Untrusted class can only open socket to the machine it was downloaded from

Authentication of Java classes

Java 1.2 (2) provides facilities for implementing

References

JDBC Tutorial
RMI Tutorial
Java beans

JavaScript
Applet tutorial

developer.netscape.com Has good reference documentation for XML, HTML
XML at W3.org
XML FAQ
XML Cover pages
Introduction to XML
CSS and XML