Title: Programming Applets How do Applets work
1Programming AppletsHow do Applets work ?
This is an HTML page
It has a link to an applet
This is the applets code
2How 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
3How do I link an Applet in my HTML Page ?
ltHTMLgt ltHEADgt ltTITLEgt My first Applet
lt/TITLEgt lt/HEADgt ltBODYgt ....... Here is how you
attach an applet to your html page!!! ltAPPLET
CODE MyApplet.class WIDTH150
HEIGTH25gt lt/APPLETgt ..... lt/BODYgt
You must provide an Applet which is programmed in
a file named MyApplet.java and compiled just as
any other class
4How do I Program an Applet ?
Lets 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 !!
5Remember 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) set
Background(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 " (n1))
)
6The 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
7Secutiry 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
8Reading 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 !!)
9 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.getInputStr
eam())) String line while ((line
in.readLine()) ! null) System.out.prinln(line
) c.close() catch(MalFormedURLE
xception e) System.out.println(yo
ur given URL was bad)
10 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 hosts address and
the port number where the server is listening
11The 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 !!!
12Sockets 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
13Socket 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(args0,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 " args0) - catch (IOException e)
- System.err.println(
- "Couldn't get I/O for the
connection to "args0) - ......
14A File Server and File Client Situation
1) Filename from keyboard
2) Request file
4) Send file
3) Read File
5) Write file
Repeat 3,4,5 until all the file is transmitted
ArchCliente.java
ArchServer.java
15Nelson, if you still have time please tell the
students about the remote ObjectsExample is
programmed in Numero.java, NumeroImpl.java and
ClienteNumero.java