Download APPLET

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

IEEE 1355 wikipedia , lookup

Remote Desktop Services wikipedia , lookup

Lag wikipedia , lookup

Transcript
Programming Applets
How do Applets work ?
This is an
HTML page
It has a link to an applet
This is the
applet’s
code
How can I explain what is an
Applet ?
• For people like you, who already master
Java programming it is very easy: an applet
is a Panel in an HTML page in which I can
program (almost) everything I can program
in a normal Panel.
Bla Bla Bla Bla
The html viewer
The applet
Bla Bla Bla Bla
How do I link an Applet in my
HTML Page ?
<HTML>
<HEAD>
<TITLE> My first Applet </TITLE>
</HEAD>
<BODY>
.......
Here is how you attach an applet to your html page!!!
<APPLET CODE = “MyApplet.class” WIDTH=150 HEIGTH=25>
</APPLET>
.....
</BODY>
You must provide an Applet which is programmed in a file
named MyApplet.java and compiled just as any other class
How do I Program an Applet ?
Let´s start with a very simple one
import java.applet.*;
import java.awt.*;
public class MyApplet1 extends Applet {
public void paint(Graphics g) {
g.drawString(“Hello World”,50,25);
}
}
This tells us the applet is a kind of frame !!
Remember the Jalisco Program ?
import java.awt.*;import java.applet.*;import java.awt.event.*;
public class Jalisco1 extends Applet {
Label question = new Label("Enter a number :");
Label answer = new Label("
");
TextField number = new TextField(9);
public void init() {
add(question);
add(number);
add(answer);
setBackground(Color.green);
number.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent x) {
String s = number.getText();
int n = Integer.parseInt(s);
answer.setText("I win with the " + (n+1));
}
});
}
}
The applet has a life cycle
• When an applet is loaded
– The applet initializes itself, running the init() method if
provided
– The applet starts running, executing the start() method
if provided
• When you leave and return to the page
– when leaving the page (going to another page) the
applet stops itself, executing the stop() method before if
provided
– when returning to the page it srarts again executing the
start() method
• When quitting the browser:
– the applet can run the destroy() method if provided
Secutiry with applets
• The applet is a running program in my computer
which I just downloaded from the network: how
can I be sure it will not harm my computer ?
– Applets cannot read or write the disks of the host
computer !
– Because java has not pointer arithmetic a class
generated by the compiler cannot read or write other
memory than just the memory of the program (in C we
have this problem !!)
– In order to avoid Trojan Horses, every applet is
“signed” by the compiler. This means, if a .class file
was not generated by the compiler, it can be easily
detected by the applet loader of the viewer and it
doesn't allow it to run
Reading the content of and URL
• An URL is a Universal Resource Locator, this means it
is a protocol to locate any resource (usually a file) that
someone has put on a WWW server anywhare.
• Normally we use a WWW-Navigator (such as
Netscape or Explorer) to download a resource.
• With java, it is possible to read directly the content of
an urn located in any WWW server of the internet.
• The next example shows how to read the file given the
URL of the resource
• In this example we assume the content is text
(otherwise read it as bytes !!):
import java.net.*;
import java,io.*;
public class Leer URL {
public static void main(String args[]) {
try {
URL miURL = new URL(“http://www.dcc.uchile.cl”);
URLConnection c = miURL.openConnection();
BufferedReader in = new BufferedReader (
new InputStreamReader(
c.getInputStream()));
String line;
while ((line = in.readLine()) != null)
System.out.prinln(line);
c.close();
catch(MalFormedURLException e) {
System.out.println(“your given URL was bad”); }
}
}
The Socket
• A socket is an endpoint of a communication link
between 2 programs running on the internet. A
socket is always bound to a port (identified by a
number) This is how the data coming from the
port can be delivered to the right program.
• A communication with sockets starts when the the
servers listen to a port expecting requests from
clients.
• A client tries to make a connection with a
rendezvous request knowing the host’s address and
the port number where the server is listening
The Socket (2)
• If everything goes right, a new port on the server is
created and a link between both programs is
established.
• This link is bi-directional, this means data can flow
in both directions.
• Client and server must open a input-stream of data
and output stream of data. They can read and write
data as the data stream were attached to a a file !!!
Sockets on the Server side
public class ArchServidor {
public static void main(String[] args) throws IOException {
Socket cs = null;
ServerSocket ss = new ServerSocket(4444);
System.out.println(”waiting for a client");
cs = ss.accept();
BufferedReader inSocket = new BufferedReader(
new InputStreamReader(cs.getInputStream()));
PrintWriter outSocket = new PrintWriter(
cs.getOutputStream(),true);
Now the program can read lines from the inSocket and
write lines to the outSocket
Socket at the Client
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
Socket aSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try { aSocket = new Socket(args[0],7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new
InputStreamReader( aSocket.getInputStream( ) ) );
} catch (UnknownHostException e) {
System.err.println("Don't know about host: "+ args[0]);
} catch (IOException e) {
System.err.println(
"Couldn't get I/O for the connection to: "+args[0]); }
......
A File Server and File Client Situation
1) Filename from keyboard
2) Request file
4) Send file
5) Write file
3) Read File
Repeat 3,4,5 until
all the file is transmitted
ArchCliente.java
ArchServer.java
Nelson, if you still have time
please tell the students about the
remote Objects
Example is programmed in
Numero.java, NumeroImpl.java
and ClienteNumero.java