Download Programming in Java

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
no text concepts found
Transcript
Lecture13
Java Applet
Programming in Java
13-1
Objectives
1. Introduction to Applet
2. Running of Applet
3. Applet tags in HTML
4. Environment of Applet running
5. Both Application and Applet
Programming in Java
13-2
Java Technologies
Programming in Java
13-3
How Java Applets work(1)
Server
Brower
class files
Java Virtual Machine
•
•
•
•
•
•
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
Programming in Java
13-4
How Java Applets work(2)
Programming in Java
13-5
Java Applet & Java Application(1)
• 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
Programming in Java
13-6
Java Applet & Java Application(2)
Feature
Applet
Application
Starting the Java classFileName Browser Web Page
Browser Open HTML File
(from
a
command
prompt)
Program
Appletviewer htmlFileName
Parameters
from command
prompt(args[0]…)
from HTML file
(PARAM…)
Has a main()
YES
NO
Supports GUI
YES
YES
Security
Restriction
NO
YES
Programming in Java
13-7
Java Applet Characteristics
• Some applets possibilities:
Communicate with web server
Dynamic content generation
"Real-time" information
• Advantages:
• Disadvantage:
- Full GUI control possible
- Not all browsers support
all Java levels
- Good security model
- Users may have Java
support disabled
- Complete programming
language
- Component reuse possible
- Class downloads can be
time-consuming
- Security model can be
limiting
Programming in Java
13-8
Restrictions and Security(1)
Programming in Java
13-9
Restrictions and Security(2)
Programming in Java
13-10
What is Sandbox?
Programming in Java
13-11
Restrictions and Security(3)
Programming in Java
13-12
Restrictions and Security(4)
No built-in
notion of
trusted
code,each
applet runs
with
separate
permissions
Programming in Java
13-13
Permissions and Policy Files (1)
• In Java2, an object of the java.security.Policy class now
represents the security policy for all kinds of Java
programs
Permission
Code
Signed Charles applet
R/W /tmp/ and /home/
Unsigned Charles applet
Connect and receive URL
Native program
R/W /tmp/
…
…
Programming in Java
13-14
Permissions and Policy Files(2)
Programming in Java
13-15
Applet Creation and Running
•Applet
creation
import java.applet.*;
public class Hello extends Applet{ …}
•JApplet
creation
import javax.swing.*;
public class Hello extends JApplet{ …}
• 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)
Programming in Java
13-16
Applet Classes
13-17
java.lang.Object
AppletClip
AppletStub
java.awt.Component
AppletContext
java.awt.Container
java.awt.Windows
java.awt.Frame
java.awt.Panel
java.applet.Applet
javax.swing.JFrame
javax.swing.JApplet
Each Applet inherits from java.applet.Applet/javax.swing.JApplet
Programming in Java
Applet Lifecycle(1)
• 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
Programming in Java
13-18
Applet Lifecycle(2)
13-19
• Loading an Applet
Creates an instance by invoking the constructor
Browser invokes init() method
Constructor
Browser invokes start() method
• Leaving and returning to the page
Browser invokes stop() method
Browser invokes start() method
• Reloading the page
stop() - suspend any activity
destroy() - free resources (threads stopped)
Ctor-Create new Applet
(init()
start())
Programming in Java
init
start
stop
destroy
Applet Tags in HTML(1)
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
Programming in Java
13-20
Applet Tags in HTML(2)
Other Applet’s Tag
<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>
Programming in Java
13-21
Applet Tags in HTML(3)
• ARCHIVE
- list Java jar files,including classes and other resource
 using Swing; but on Java1.1
download Swing patch from network(swing.jar)
<APPLET
CODE = “CalculatorApplet.class”
ARCHIVE = “CalculatorClasses.jar,
swing.jar”
WIDTH = 100
HEIGHT = 150
Programming in Java
13-22
Transfer Parameters to Applet from HTML
• Applet can get parameters from html file
• Tag PARAM
– HTML文件
<PARAM NAME=“Param_Name” VALUE=“”>
– APPLET文件
String getParameter(“Param_Name”)
HTML
<APPLET CODE=”Demo.class” WIDTH=400 HEIGHT=300>
<PARAM NAME=”font” VALUE=”TimesRoman”>
<PARAM NAME=”size” VALUE=”20”>
</APPLET>
Programming in Java
13-24
Example
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);
}
}
Programming in Java
13-25
Interaction Between Applets in HTML
• 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()
<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>
Programming in Java
13-26
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);
Programming in Java
13-27
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()+
newLine);}
textArea.append(“-”+newLine);
}
Programming in Java
13-28
Interaction Between HTML and Applet
• 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.
Programming in Java
13-29
Example(1)
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; }
Programming in Java
13-30
Example(2)
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);
}
}
Programming in Java
13-31
Example(3)
<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>
Programming in Java
13-32
How are Java Applets Used
Programming in Java
13-33
Related documents