Java Applets

download Java Applets

of 11

description

Presentation on Java applets.

Transcript of Java Applets

Applets

AppletsIn JavaWhat is an Applet?Applets are small Java programs that are embedded in Web pages. They can be transported over the Internet from one computer (web server) to another (client computers). They transform the web into rich media and support the delivery of applications via the Internet. It is also a special Java program that can be embedded in HTML documents. A Java applet is a Java program written in a special format to have a graphical user interface. The graphical user interface is also called a GUI , and it allows a user to interact with a program by clicking the mouse, typing information into boxes, and performing other familiar actions. With a Java applet, GUIs are easy to create even if you've never run into such GUI before.

Applet and HTML file

Applet SecuritySecurity is a big concern in the Java environment because any web page a user retrieves may cause Java code to be transferred and executed. The user may not even be aware that a Java applet is running. One security checkpoint occurs as the applet is being interpreted. As each of the required classes is loaded, the JVM's Class Loader checks the code for attempts to violate security. If any are found, the applet is not allowed to run. Java applets are further prevented from tampering with a user's local machine by the "sandbox" security model, which forms a barrier between the applet and the rest of the user's system. Being contained within this barrier, the applet isn't allowed to do any of the following:Read or write to the local file systemModify or delete files from the local file systemCreate or rename directories on the local file systemList directory contents or check for the existence of a fileObtain file attribute information such as size, type, modification timeCreate network connections to computers other than the source of the appletListen or connect via ports on the local systemRead from or modify various local system propertiesHalt the execution of the Java interpreterLoad local libraries or invoke local executablesThese security restrictions do make Java applets less useful than many programmers would like. Since an applet cannot read or write files, it is not a good fit for any program that needs to take stored data, process it, and return a result. Another major drawback is that you cannot currently print the contents of a Java applet's display area.

Uses of appletsToday, applets are used mainly to add two things to the web environment: animation and interactivity. Java is ideally suited to provide fairly sophisticated image animation without having to constantly contact the server. Java also enhances interactivity by providing a way to validate user data entered into a form before it is sent off to the server. These animation and interactivity features could even be combined to provide easier navigation through a complex user interface.

Applet life cycleThese methods are known as "callback methods" as they are called automatically by the browser whenever required for the smooth execution of the applet. Programmer just write the methods with some code but never calls.

Applet Life cycle call back methodsinit()init() is first method to call when the applet is loaded.It is called exactly once in an applets life.In this method, you initialize variables, read PARAM tags, register events, set the foreground and background color of an applet.init() is not compulsory to override it.start()start() is the second method calls after the init() method.In this method you can perform work like start thread execution.start() is called each time an applets HTML document is displayed onscreen but It is called at least once in an applets life cycle.start() is not compulsory to override it.paint()paint() is used to draw output on applet window.paint() is called each time your applets output must be redrawn.This method takes a java.awt.Graphics object as parameter. This class includes many methods of drawing necessary to draw on the applet window.This is the place where the programmer can write his code of what he expects from applet like animation etc. This is equivalent to run method of thread.Applet Life cycle call back methodsstop()stop() is complement of start() method.stop() is called at least once in an applets life, when the browser leaves the page in which the applet is embedded.In this method the applet becomes temporarily inactive.An applet can come any number of times into this method in its life cycle and can go back to the active state (paint() method) whenever would like.It is equivalent to the blocked state of the thread.

destroy()destroy() is last method to called during an applet life cycle.destroy() is called exactly once in an applets life, just before the browser unload the applet.This is the end of the life cycle of applet. It is the best place to have cleanup code. It is equivalent to the dead state of the thread.

tagHTML provides an ... container tag for embedding Java applet, with the following attributes:

code="JavaClassName": Specifies the applet class name, with the ".class" extension.

width, height: Specify the width and height of the applet's display area inside the browser.

alt: (optional) alternate text if the applet fails to be displayed.

codebase: (optional) specifies the base path of the applet's class. The default base path for the applet is the same path as the HTML file.

archive: (optional) specifies the JAR file that contains the applet classes. If your applet involves more than one classes, it is common and more efficient to jar (i.e., zip) them together into a single JAR file for distribution.