Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.

Similar presentations

Presentation on theme: "Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet."— Presentation transcript:

1 Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet

2 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. The html viewer The applet Bla Bla

3 How do I link an Applet in my HTML Page ? My first Applet....... Here is how you attach an applet to your html page!!!..... You must provide an Applet which is programmed in a file named and compiled just as any other class

4 How do I Program an Applet ? import java.applet.*; import java.awt.*; public class MyApplet1 extends Applet { public void paint(Graphics g) { g.drawString(“Hello World”,50,25); } Let´s start with a very simple one This tells us the applet is a kind of frame !!

5 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(; 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)); } }); }

6 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

7 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

8 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 !!):

9 import*; import java,io.*; public class Leer URL { public static void main(String args[]) { try { URL miURL = new URL(“”); 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”); } }

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 host’s address and the port number where the server is listening

11 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 !!!

12 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

13 Socket at the Client import*; import*; 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]); }......

14 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 A File Server and File Client Situation

15 Nelson, if you still have time please tell the students about the remote Objects Example is programmed in, and

Download ppt "Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet."

Similar presentations

Ads by Google