13-1 Programming in Java Java Applet Lecture13. 13-2 Programming in Java 1. Introduction to Applet...

Preview:

Citation preview

13-1

Programming in Java

Java Applet

Lecture13

13-2

Programming in Java

1. Introduction to Applet

2. Running of Applet

3. Applet tags in HTML

4. Environment of Applet running

5. Both Application and Applet

Objectives

13-3

Programming in Java

Java Technologies

13-4

Programming in Java

Brower

Java Virtual Machine

Serverclass files

• Server delivers HTML data stream (text and tags)• Java applet is referenced in the HTML• Browser parses data and displays on Client• Browser requests applet class file(s)• Browser loads applet into JVM• JVM executes applet code

How Java Applets work(1)

13-5

Programming in Java

How Java Applets work(2)

13-6

Programming in Java

• Applet

- A Java program that is referenced by a Web page and runs inside a Java-enabled Web browser

• Application

- A stand-along Java program that runs via the “java” command

Page Brower

VM

Applet

VM

Application

Java Applet & Java Application(1)

13-7

Programming in Java

Feature Application Applet

Starting the Program

Java classFileName(from a command prompt)

Browser Web PageBrowser Open HTML FileAppletviewer htmlFileName

Parameters from command prompt(args[0]…)

from HTML file(PARAM…)

Has a main() YES NO

Supports GUI YES YES

SecurityRestriction

NO YES

Java Applet & Java Application(2)

13-8

Programming in Java

• Some applets possibilities:

Communicate with web server Dynamic content generation "Real-time" information

• Advantages:

- Full GUI control possible

- Good security model

- Complete programming language

- Component reuse possible

• Disadvantage:

- Not all browsers support all Java levels

- Users may have Java support disabled

- Class downloads can be time-consuming

- Security model can be limiting

Java Applet Characteristics

13-9

Programming in Java

Restrictions and Security(1)

13-10

Programming in Java

Restrictions and Security(2)

13-11

Programming in Java

What is Sandbox?

13-12

Programming in Java

Restrictions and Security(3)

13-13

Programming in Java

Restrictions and Security(4)

No built-in notion of trusted

code,each applet runs

with separate

permissions

13-14

Programming in Java

• In Java2, an object of the java.security.Policy class now represents the security policy for all kinds of Java programs

Signed Charles applet

Unsigned Charles applet

Native program

R/W /tmp/

Connect and receive URL

R/W /tmp/ and /home/

Code Permission

Permissions and Policy Files (1)

13-15

Programming in Java

Permissions and Policy Files(2)

13-16

Programming in Java

Applet Creation and Running

• Browers which support Java(HotJava 、 Netscape Navigator 、 IE)

• Applet viewer(\jdk\bin\)– Applet viewer [option] urls…

– Only can read tags such as OBJECT 、 EMBED 、 APPLET ( can’t read other HTML tags )

import javax.swing.*;public class Hello extends JApplet{ …}

•Applet creation

•JApplet creation

import java.applet.*;public class Hello extends Applet{ …}

13-17

Programming in Java

Applet Classes

javax.swing.JFrame

java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Windows java.awt.Panel

java.awt.Frame java.applet.Applet

javax.swing.JApplet

AppletStub

AppletClipAppletContext

Each Applet inherits from java.applet.Applet/javax.swing.JApplet

13-18

Programming in Java

• init()-Invoked at applet downloaded and implement to transform paras from HTML to Applet (initialization)

• start()-Invoked at viewing the page, and could be invoked again and again

• stop()-Invoked at leaving the page, and could be invoked again and again also

• destroy()-Invoked at closing the browser

Applet Lifecycle(1)

13-19

Programming in Java

Constructor

start

stop

destroy

init

• Loading an AppletCreates an instance by invoking the constructor Browser invokes init() methodBrowser invokes start() method

• Leaving and returning to the pageBrowser invokes stop() methodBrowser invokes start() method

• Reloading the pagestop() - suspend any activitydestroy() - free resources (threads stopped)Ctor-Create new Applet (init() start())

Applet Lifecycle(2)

13-20

Programming in Java

Necessary Tags

• Applet’s Tag in HTML<APPLET CODE=

WIDTH=

HEIGT= >

</APPLET>

• Three information about Applet

- Name of Class file

- Location of Class file

- How to representation Applet in page

Applet Tags in HTML(1)

13-21

Programming in Java

Other Applet’s Tag

Applet Tags in HTML(2)

<APPLET

[CODEBASE = codebaseURL] CODE=appletClassFile [ALT=alternativeText] [NAME=appletInstanceName] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment ] [VSPACE = pixels] [HSPACE = pixels]> [<PARAM NAME=AttrName VALUE =AttrValue>] [<PARAM NAME=AttrName2 VALUE =AttrValue>] …… [HTML displayed in the absence of Java] </APPLET>

13-22

Programming in Java

<APPLET

CODE = “CalculatorApplet.class”

ARCHIVE = “CalculatorClasses.jar,

swing.jar”

WIDTH = 100

HEIGHT = 150

using Swing; but on Java1.1

download Swing patch from network(swing.jar)

• ARCHIVE

- list Java jar files,including classes and other resource

Applet Tags in HTML(3)

13-24

Programming in Java

• Applet can get parameters from html file

• Tag PARAM

– HTML 文件 <PARAM NAME=“Param_Name” VALUE=“”>

– APPLET 文件 String getParameter(“Param_Name”)

<APPLET CODE=”Demo.class” WIDTH=400 HEIGHT=300>

<PARAM NAME=”font” VALUE=”TimesRoman”><PARAM NAME=”size” VALUE=”20”>

</APPLET>

HTML

Transfer Parameters to Applet from HTML

13-25

Programming in Java

Java Code

public class Demo extends Applets {

public void paint(Graphics g){

String fontName=getParameter(“font”);

String sizeName=getParameter(“size”);

int fontSize=Integer.parseInt(sizeName);

Font fnt=new Font(fontName,Font.BOLD,fontSize);

g.setFont(fnt);

g.drawString(“Demo”,40,50);

}

}

Example

13-26

Programming in Java

• Get the HTML runtime environment

– getAppletContext() [java.applet.Applet]

• Get an Applet

– public Applet getApplet(String name);

• Get all Applets on the same HTML

– public Enumeration getApplets()

Interaction Between Applets in HTML

<html>

<applet code=“Sender.class” WIDTH=400 HEIGHT=200 name=“Wang”> </applet> <p>

<applet code=“Receiver.class” WIDTH=400 HEIGHT=200 name=“Li”> </applet> <p>

<applet code=“GetApplets.class” WIDTH=400 HEIGHT=200> </applet>

</html>

13-27

Programming in Java

Example(1)

import java.applet.*;

import java.awt.*;

import java.awt.event;

import java.util.Enumeration;

import javax.swing;

public class GetApplets extends JApplet implements ActionListener

{ private TextArea textarea;

private String newline;

public void init()

{ container contentPane= getContentPane();

Button b=newButton(“Click to call getApplets()”);

b.addActionListener(this);

contentPane.add(“North”,b);

13-28

Programming in Java

Example(2)

textArea = new TextArea(5,40); textarea.setEditable(false); contentPane.add(“center”, textArea); newLine=System.getProperty(“line.separator”);}public void actionPerformed(ActionEvent e){ printApplets(); }public void printApplets(){ Enumeration e=getAppletContext().getApplets(); textArea.append(“Result:” + newLine); while (e.hasMoreElements()) {Applet applet=(Applet)e.nextElement(); textArea.append(“-”+applet.getClass().getName()+ newLi

ne);} textArea.append(“-”+newLine);}

13-29

Programming in Java

• Get Applet URL from browser

– URL getCodeBase() • Get HTML URL which embedded Applet from browser

– URL getDocumentBase()• Get PARAM value by name inHTML

– String getParameter(String name)• Get Applet receiving parameters

– String[] getParameterInfo()• Get this Applet information,such as applet designer,version

– String[] getParameterInfo()

• Show a new web page in the browser

– Use method ShowDocument() of Class AppletContext

– ShowDocument() have two parameters, first is URL, second is where display new page.

Interaction Between HTML and Applet

13-30

Programming in Java

import java.applet.*;

import java.awt.*;

import java.net.*;

public class Bookmark extends Applet {

private List links;

public boolean action(Event evt, Object arg) {

if(evt.target==links)

{ try{ AppletContext context=getAppletContext();

URL u = new URL((String)arg);

context.showDocument(u,"right");}

catch(Exception e){showStatus("Erro"+e); }}

else{ return super.action(evt,arg); }

return true; }

Example(1)

13-31

Programming in Java

public void init() {

setLayout(new BorderLayout());

links=new List(5,false);

int i=1;

String s;

while ((s=getParameter("link_"+i))!=null){

links.addItem(s);

i++; }

add("Center",links);

}

}

Example(2)

13-32

Programming in Java

<HTML>

<TITLE>One Simple Bookmark</TITLE>

<FRAMESET COLS-"300,*" cols="42%,*">

<FRAME NAME="left" MARGINHEIGHT=2 MARGINWIDTH=2 SCROLLING="no" NORESIZE src="URLlist.htm">

<FRAME NAME="right" MARGINHEIGHT=2 MARGINWIDTH=2 SCROLLING="yes" NORESIZE src="URLshow.htm">

</FRAMESET>

</HTML>

Example(3)

13-33

Programming in Java

How are Java Applets Used

Recommended