28
OOP&M - laboratory lectur es 1 JPN – GUI and Applets “… I am sure ‘it wont fail as much as the one before …” - Bill Gates about W98 -

OOP&M - laboratory lectures1 JPN – GUI and Applets “… I am sure ‘it wont fail as much as the one before …” - Bill Gates about W98 -

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

OOP&M - laboratory lectures 1

JPN – GUI and Applets

“… I am sure ‘it wont fail as much as the one before …”- Bill Gates about W98 -

OOP&M - laboratory lectures 2

JPN – Java and the Net

Clients and ServersOne of the reasons of the interest in Java is its delivery mechanism. Java applets do not reach the user as a fixed and closed product into a floppy or CD. Nor they are downloaded only once from the net to be used over and over again. Instead, they are embedded within a web page and downloaded anew with each loading of the web page.

“Java applets are not only on the Internet, but are creatures of the net as well”

- Arnow/Weiss -

OOP&M - laboratory lectures 3

JPN – Java and the Net

Internet Communication

Communication on the internet can be defined as the transfer of data from one machine to another.Into this definition there are many different concepts that fit:

-Email-Remote logins-Download of web pages and applets-Chat rooms-…

Information is break into small pieces called packets and transferred in that form

OOP&M - laboratory lectures 4

JPN – Java and the Net

Internet Communication

The machine that is sending the information is almost never connected directly to the receiver, so these packets are actually transferred from one machine to another through the internet until they arrive at their destination

The machines responsible of this transfer of data are called routers and are dedicated only to this purpose. Therefore they use a special software and have special form (and are quite expensive … go to www.cisco.com and check it out)

The transfer of packets is not secure, they can get lost in the way. The connection between machines is not good enough, and the machines themselves have limited memory … if packets arrive at rate higher than machine’s process rate, they could be lost

OOP&M - laboratory lectures 5

JPN – Java and the Net

Internet Communication

Network A

Computer 1

Computer 2 Computer 3N

etw

ork

B

Computer 4

Computer 5

Computer 6

R2

R6

R4

R5R1 R3

Computer 7

Computer 8 Computer 9

Computer 10

Network C

OOP&M - laboratory lectures 6

JPN – Java and the Net

Internet Communication

Network A

Computer 1

Computer 2 Computer 3N

etw

ork

B

Computer 4

Computer 5

Computer 6

R2 R4

R5R1 R3

Internet Communication is:– One way– Broken into packets– Unreliable (packets can get lost)– Transmitted over many hops

OOP&M - laboratory lectures 7

JPN – Java and the Net

Internet Communication

Network A

Computer 1

Computer 2 Computer 3N

etw

ork

B

Computer 4

Computer 5

Computer 6

R2 R4

R5R1 R3

With TCP it appears to be:– Bidirectional– A continuous Stream– Reliable– Direct

OOP&M - laboratory lectures 8

JPN – Java and the Net

Internet Communication

Every router must be connected, at least, to another router, so that there will always be a path of routers between any couple of machines residing in different networks.

The fact of the looses in terms of packets makes the network a not useful tool for almost any kind of applications. But there is a set of tools, which are the communication protocols, in order to make possible, the use of the net a communication medium.

First, the “Internet Protocol” is running in all the machines at the net. Our applications use the “Transfer Control Protocol”. Both observe how the packets are transferred through the network and forces the sender to repeat if any loose of data is detected.

THIS EMULATES A DIRECT STREAM OF DATA BETWEEN TWO M.

OOP&M - laboratory lectures 9

JPN – input / output to programs

Javaprogram

keyboard

monitor

files

network

InputStream

System.out

FileOutputStream

Javaprogram

FileInputStream

System.in

InputStream

OutputStream

OOP&M - laboratory lectures 10

JPN – input/output … an overview

Javaprogram

InputStrea

m

bytes of data

ewsc24rfds53Hej20Hur20m76ar20du20?rsf

Input source

We must build a bridge between the Java Program and the input source!!

OOP&M - laboratory lectures 11

Stages

JPN – input … as example

•Construct a reference to an InputStream object, and with it•Construct an InputStreamReader object, and with it•Construct a BufferedReader object

This references could be of two types•FileInputStreamReader for files•BufferedInputStream for keyboard and the network

Note that:•InputStream inputs sequences of bytes into the program•InputStreamReader models the stream of input as

characters, but doesn’t recognize ends of line a.o.•BufferedReader has got a collection of methods that allow

us to work in a similar way as we did with PrintStream

OOP&M - laboratory lectures 12

JPN – a board with keys, the example

Java provides a predefined object to represent the stream of input that comes from the keyboard. System.in is a reference to this object contained into the BufferedInputStream class

Unlike System.out, which refers to a PrintStream object and therefore can be used right away to write Strings to the monitor, System.in, a reference to a BufferedInputStream object, cannot be readily used to read Strings

keyboard

[existing] InputStream

new InputStreamReader

new BufferedReader

keyb

isr

System.in

OOP&M - laboratory lectures 13

the constructor for InputStreamReader accepts the keyboard (a BufferedInputStream reference) as its argument

new InputStreamReader (System.in)

Java provides System.in contained into the BufferedInputStream class

the constructor for BufferedReader accepts a InputStreamReader reference as its argument

new BufferedReader (ISR)

JPN – a board with keys, the example

OOP&M - laboratory lectures 14

The total declaration would be:

InputStreamReader isr;BufferedReader keyb;

isr = new InputStreamReader(System.in);keyb = new BufferedReader(isr);

And now, for reading a line:

InputStreamReader isr;BufferedReader keyb;String inputline;

isr = new InputStreamReader(System.in);keyb = new BufferedReader(isr);inputline = keyb.readLine();

*.readLine() is a method from the BufferedReader class

JPN – a board with keys, the example

OOP&M - laboratory lectures 15

Example: a program that writes the “plural” of a word

import java.io.*;/* * This program writes the plural of the word typed in the * keyboard … it just adds an “s”*/class plural { public static void main(String[] arg) throws Exception{ InputStreamReader isr; BufferedReader keyb; String inputline;

isr = new InputStreamReader(System.in); keyb = new BufferedReader(isr); inputline = keyb.readLine(); System.out.print(inputline); System.out.println(“s”); }}

JPN – a board with keys, the example

OOP&M - laboratory lectures 16

JPN – come IN PUT files

Obtaining input from disk files is only a little bit more complicated than from the keyboard. Our starting point must be to find some sort of InputStream object. As we said before, both BufferedInputStream and FileInputStream belong to that class

new FileInputStream

new InputStreamReader

new BufferedReader

bsr

isr

System.in is already defined into the class BufferedInputStream, the problem is that the FileInputStream class does not have defined objects like that. Therefore we need to first define an object that makes reference to a file.

fsr

files

OOP&M - laboratory lectures 17

JPN – come IN PUT files

the constructor for InputStreamReader accepts a stream from a file (a FileInputStream reference) as its argument

new InputStreamReader (FileInputStream)

the constructor for FileInputStream accepts a file (a File reference) as its argument

the constructor for BufferedReader accepts a InputStreamReader reference as its argument

new BufferedReader (ISR)

new FileInputStream (file)

OOP&M - laboratory lectures 18

JPN – come IN PUT files

The total declaration would be:

File f;FileInputStream fsr;InputStreamReader isr;BufferedReader bsr;

f = new File(“Big_parties_many_potatis.txt”);fsr = new FileInputStream(f);isr = new InputStreamReader(fsr);bsr = new BufferedReader(isr);

OOP&M - laboratory lectures 19

JPN – come IN PUT files

Let’s read a couple of lines from the file and print them:

File f;FileInputStream fsr;InputStreamReader isr;BufferedReader bsr;String inputline;

f = new File(“Big_parties_many_potatis.txt”);fsr = new FileInputStream(f);isr = new InputStreamReader(fsr);bsr = new BufferedReader(isr);inputline = bsr.readLine();System.out.println(inputline);inputline = bsr.readLine();System.out.println(inputline);

OOP&M - laboratory lectures 20

JPN – Streams from the internet•When we are try to read information from the Web •The Net as other communication media for our programs

can be read using Streams•There is a difference between what we saw until now and

what will happen with the Net: files are in HTML code•The constructors are:

String s;URL u;InputStream ins;InputStreamReader isr;BufferedReader link;

u = new URL("http://www.domain.ext/");ins = u.openStream();isr = new InputStreamReader(ins);link = new BufferedReader(isr);

s = link.readLine();

As you see here, the structure is similar to the one that we saw for the files. The difference is at the constructor for the InputStream object, which is special!! But the method for reading the information is the same: *.readline()

OOP&M - laboratory lectures 21

import java.net.*;import java.io.*;

class MyFirstBrowser { public static void main(String[] arg) throws Exception { URL uNet; InputStream insNet; InputStreamReader isrNet; BufferedReader linkNet;

String s;

uNet = new URL("http://webzone.k3.mah.se/k3dacu/oopm/week5/index1.html"); insNet = uNet.openStream(); isrNet = new InputStreamReader(insNet); linkNet = new BufferedReader(isrNet);

s = linkNet.readLine(); System.out.println(s); s = linkNet.readLine(); System.out.println(s);

System.out.println("connection closed"); }}

Example – Just reading a file

JPN – Streams from the internet

OOP&M - laboratory lectures 22

•Features needed:•Interpret HTML TAGs

•underline a text if it is a link (<A HREF..>)•jump line with <P> <BR>•show the name of the images (<IMG SRC...>)•try to show tables (<TABLE><TR><TD>...)•think how to ask to the user for the next jump to do ...

•Interpret scripting languages•JavaScript•Visual Basic Script•Perl

•Calls external applications•Office•Java Virtual Machine

JPN – internet

The Browser is much more than only that

OOP&M - laboratory lectures 23

JPN – Java and the Net

Sockets

A TCP connection between two programs in many ways is like a phone connection. Data can move in both directions simultaneously. But, before the communication act takes place, the connection must be established one party must call the other

Both parties need something beside, like in a phone call, both speakers have a telephone. The analogy between the telephone and the programming world is the socket. It is to the communication between computers like the telephone between two persons

In Java we model the telephone with an object of the Socket class. A socket is created for making a single TCP connection

OOP&M - laboratory lectures 24

JPN – Java and the Net

Sockets

The arguments to the constructor of the socket are the machine to contact and the port where the communication will take place. The port could be understood as the extension to the phone number in a large phone system

new Socket(“www.yahoo.com”, 80);

The socket provides the methods: *.getInputStream() and *.getOutputStream(), from whose return values BufferedReader objects and PrintStream objects can be constructed. These objects use the TCP connection as their input source or output target

OOP&M - laboratory lectures 25

JPN – Java and the Net

Sockets

The typical example would look like:

import java.io.*;import java.net.*;

Socket s = new Socket(hostname, portnumber);BufferedReader br = new BufferedReader(

new InputStreamReader(s.getInputStream()));

PrintStream ps = new PrintStream(s.getOutputStream());

String line = br.readLine(); //reads from the host

ps.println(“hej”); //writes to the host

OOP&M - laboratory lectures 26

JPN – Java and the Net

Client-Server Computing

Sockets and TCP/IP connections allow programs to communicate throw the internet, but they don’t determine any particular style of communication or relationship between the programs. There is a widely used design approach for Internet applications - the so called client-server model –

In this model, an application consist of one program running as server, and another as a client

Server: a program that provides a service

Client: a program that requests a service

OOP&M - laboratory lectures 27

JPN – Java and the Net

Client-Server Computing

Client an server converse, what means that they follow some communication rules. From the point of view of the client, what happens is:

1. Creates a socket object with the machine address and port to the server

2. Sends a message to the server on the TCP connection associated to the socket

3. Waits for response from the server that will be:• More information needed• Requested action performed• The requested data

Each communication is established (1) only once, but the conversation can be repeated (2-3) many times

OOP&M - laboratory lectures 28

JPN – Java and the Net

Client-Server Protocols

Each client-server application has its own rules (or protocol) governing the conversation between client and server

The web clients and servers use one protocol called HyperText Transport Protocol (HTTP)

Email clients and servers use a different protocol, called Simple Mail Transfer Protocol (SMTP) if we send mail or POP if we receive (there are other variants, like IMAP, a.o.)