200 likes | 393 Views
Applets. applets/applications overview HTML applets applet futures examples. Applet Overview. application: A class containing a method that is declared static public void main(String [] args) { applet: A class derived from java.applet.Applet
E N D
Applets applets/applications overview HTML applets applet futures examples
Applet Overview • application: A class containing a method that is declared static public void main(String [] args) { • applet: A class derived from java.applet.Applet • java.lang.Applet is not an interface. Thus single inheritance says that your applet may not extend any other classes.
Applets and HTML • Intent of applet design was to give dynamic content to Web pages by running on a virtual machine built into a web browser. • Web browser’s virtual machine uses a security manager to deny any machine operations that could do anything harmful, and restricts access within the applet to the web server (to dynamically download classes as needed)
HTML • Web servers/clients • client requests browser to receive a page using a URL that specifies the protocol (http), a location (www.eng.auburn.edu), a port, and a page http://www.eng.auburn.edu:80/department/cse/classes/cse525/index.html • index.html is a page indicating presentation formatting (html) to a browser.
HTML • HTML is an application of SGML (Standard Generalized Markup language) • Line breaks and indentation in a web page have no meaning and exist solely for human readability. • Browsers format web pages using tags which are indicated by surrounding a name in <name > and closed with a tag </name>
HTML • tags such as <P> indicate a new paragraph (no </P> required since the next <P> tag closes the previous <P>) • Tags <A href=“some url here”> visible link </a> allow a web page to request the download of yet another page. • For our purposes, we are interested only in putting java programs on HTML pages.
HTML • Applet tags were introduced into HTML to indicate the inclusion of a java program. • A minimal HTML page containing an applet would be: <HTML><BODY> <APPLET code=“name.class” height=value width = value> </APPLET> </BODY></HTML>
HTML Example <HTML><HEAD> <TITLE>Slide Viewer Applet</TITLE> <!-- Changed by: W. H. Carlisle, Sept. 1998 --> </HEAD> <CENTER> <H2> Auburn Images </H2> </CENTER> <BODY BGCOLOR="#000000" text="#ffffff" link="#00caFF" > <CENTER> <APPLET CODE=SlideProjector.class WIDTH=250 HEIGHT=450 ALIGN=TOP> <PARAM NAME=width VALUE=250> <PARAM NAME=height VALUE=450>
HTML <PARAM NAME=FONT VALUE="Helvetica"> <PARAM NAME=FONTHEIGHT VALUE=18> <PARAM NAME=TRANSITIONDELAY VALUE=2000> <PARAM NAME=LEFTARROWIMAGE VALUE="forward.gif"> <PARAM NAME=RIGHTARROWIMAGE VALUE="backward.gif"> <PARAM NAME=TITLEIMAGE VALUE="title.gif"> <PARAM NAME=SEPARATOR value="|"> <PARAM NAME=SLIDE0 VALUE="3.JPEG|3.JPEG"> <PARAM NAME=SLIDE1 VALUE="4.JPEG|4.JPEG"> <PARAM NAME=SLIDE2 VALUE="6.JPEG|6.JPEG"> <PARAM NAME=SLIDE3 VALUE="8.JPEG|8.JPEG"> </APPLET><BR> </CENTER>
Applet life cycle • the browser downloads the class in the applet tag and calls an init() method. This is where you initialize your program • the browser calls the start() method. This method starts your program running, and will be called whenever the applet needs restarting.
Applet life cycle • The stop() method is called when the web page is replaced by another page. • Before a browser terminates an applet it will call the destroy() method of the applet. • It is not required that methods of your applet override any of these methods.
Applet inheritance • Your applets extend Applet which has methods other than start, stop….methods providing functionality that is useful to programs in web pages. • Every applet “is a Panel” which is a Container which is a Component which is an Object meaning that applets inherit methods from these classes as well (e.g. paint() )
Applet methods • Those that interact with browser • URL getDocumentBase() returns a URL object that has methods that allow you to deal with the base of the web page • URL getCodeBase() contains the URL of the applet, which could differ from the Document base. • String getParameter(String name) returns the value of a parameter in the applet tag.
Applet methods • Those that provide Media Support • Image getImage(URL) • Image getImage(URL,name) • AudioClip getAudioClip(URL url), .. • void play(URL), ...
Applet methods • Those that interact with the browser environment • AppletContext getAppletContext() returns an instance of the AppletContext class that allows an applet to affect its environment in limited ways • void showDocument(URL) replaces the current web page with the URL. • ...
Applet methods • Those that provide applet information • String getAppletInfo() • … • Of the methods in ancestor classes, the paint(Graphics g) method is probably the most useful, as overriding this method gives the programmer a Graphics object that can be used to draw in the applet.
Current applet issues • Classes used in applet programs are loaded individually in order as needed. • This is both a strength • (don’t need 100 Mbytes of class so never loaded) and a weakness • ( loading a class when needed takes a performance hit)
Approaches • load a basic collection of classes to get started, and while the user is interacting with the basic interface,other classes in another thread with lower priority can download other classes. • When you wish to load a large number of classes at one time, use JAR files.
Jar files • utility named jar creates a gzipped file of classes. (Jar files can be dealt with by utilities such as winzip) but have the command line semantics of “tar” • jar cf name.jar *.class image.gif sound.au • <applet archive=“name.jar” code=“whatever.class” height= width= >
Another current applet issue is security • Applets are insecure • Applications have no restrictions. • Digital signatures may be attached to jar files. With this mechanism an applet may be “trusted” • Trusted applets can be dealt with by a Security manager allowing more access.