The oldest form of plugin,the Java applet has recently been redefined to work with the generic browser plugin framework. Previously,applets had to be loaded using the <applet/> element. HTML 4.0 deprecated <applet/>,favoring <object/> as the sole means for embedding plugins.To aid in this move, Sun Microsystems created the Java Plugin, which is included as part of the Java Runtime Environment (JRE) .
Embedding an applet requires you to use the nonstandard code property of <object/> to specify the class to load. Although all browsers should work when using data for the same purpose,the reality is that support isn’t yet universal. So,in the interest of cross-browser compatibility,it’s best to use code in this way:
Note the mime type for Java applets is application/x-java-applet.By specifying this, you are sure that the browser will use the most appropriate (and available) version of the Java plugin.You can optionally specify the exact version required by adding it to the end of the mime type. For example, to specify version 1.4.2 (from JRE 1.4.2), add “jpi-version=1.4.2”:
In this mime type, JPI is short for Java PlugIn and it ensures that the applet is not run unless the plugin on the user’s computer is exactly equal to version 1.4.2.So even if the user has version 1.4.3, the applet does not run. For this reason,it’s best to omit the plugin version to avoid any annoyance for your users.
The applet class can be contained within a JAR (Java Archive) file. In that case, specify the JAR file by using the archive attribute:
To support Netscape Navigator 4.x, you use the original <applet/> element.To ensure that the code works in all browsers, you can embed <applet/> inside of <object/>(similar to the way you can place <embed/> inside of <object/>).However, doing so requires the use of the IE-proprietary <comment/> element. Although Mozilla and other browsers ignore content inside of <object/>, IE doesn’t, and could end up rendering two copies of the same applet. Inserting the <comment/> element tells IE to ignore the extra content:
However, if you use the <object/> element to embed applets, the document.applets collection doesn’t include it. When using <object/>, you can access the applet using document.getElementById():var oApplet = document.getElementById(“ExampleApplet”);
If you are using both <object/> and <applet/> for compatibility with older browsers, you should use a function to determine the appropriate method:
To write a Java applet, you must first download the Java Development Kit (JDK) from Sun’s Web site (http://java.sun.com/j2se/). It is up to you whether you use a development environment or a plain text editor to write the applet, but all applets have one thing in common: they must inherit from java.applet.Applet.(You can, however, create a Swing-based Java applet by inheriting from javax.swing.JApplet, which inherits from java.applet.Applet).
Here’s a small example applet:
This applet simply displays the text “Hello World!” on the applet. The paint() method controls what is displayed when the applet is first loaded, and it receives a Graphics object as its sole argument. The Graphics object is a representation of the visual area of the applet with methods to draw onto the applet, such as drawString(),which draws the given text at the x and y coordinates specified.
If, however, the applet is to display something, you most likely need to defined a paint() method.
After the applet is defined and saved in a file with a .java extension,compile the file using the javac utility from the command line:javac ExampleApplet.java
This command creates a file with a .class extension (the previous example creates
ExampleApplet.class). The .class file must be placed in a Web server directory to allow HTML pages to access it.
This example presents the user with a text box where a new message can be entered. When the user clicks the Set Message button, it calls the changeAppletMessage() function, which gets a reference to the applet and retrieves the text from the text box. Then, the function calls the applet’s setMessage() method, passing in the entered text.The message displayed in the applet changes from “Hello World!” to whatever the user enters (sometimes this happens quickly, sometimes slowly).
Handling Java exceptions
Suppose you change the ExampleApplet to throw an error if setMessage() is passed a zero-length string, such as:
First, applets are not allowed access to the user’s file system. This prevents a major security problem if a malicious applet writer gets an unsuspecting user to open a page containing the applet.By default, this isn’t possible.
Second, applets aren’t allowed to access resources across domains. This is the same security restriction placed on the XML HTTP requests discussed earlier.
It is possible to get around these restrictions by digitally signing the applet. When an applet is signed, a dialog is presented to the user asking whether the signature is valid and, in turn, whether the applet should be allowed enhanced privileges not available otherwise. If the signature is accepted, the restrictions mentioned previously are lifted.
The JSObject class has the following methods:
In Java, it becomes a bit more involved:
Here’s an example applet that makes use of the LiveConnect package:
When including an applet that uses the LiveConnect package, you must set a special parameter to allow it access to the HTML document. The parameter’s name is mayscript, and it should be set to true:
If you are using the old <applet/> element, then just include mayscript as an attribute:
Back to the example,the getMessageFromApplet() function does nothing more than display the argument that was passed. Here’s the complete HTML code for the page:
When this page is loaded, the applet displays the page title (Applet Example) and the URL you are viewing the example from. Then, you should see an alert displaying the message “Applet says:Hello from the Java applet!”
You can also use call() to execute getMessageFromApplet():
Because getMessageFromApplet() is a global function, it’s considered a method of window and, therefore, can be called using the call() method.
Java Script Related Tutorials
|Adv Java Tutorial||Core Java Tutorial|
|Java-Springs Tutorial||Java Servlets Tutorial|
|EJB(Enterprise JavaBeans) Tutorial||JavaServer Faces (JSF) Tutorial|
|Java Swing Tutorial||Java Tutorial|
|JavaMail API Tutorial||Java 8 Tutorial|
|Java XML Tutorial||Java Bean Utils Tutorial|
|The Java Debugger (JDB) Tutorial||Java.math Package Tutorial|
Java Script Related Interview Questions
|Java Script Interview Questions||Adv Java Interview Questions|
|Core Java Interview Questions||Java-Springs Interview Questions|
|Java Servlets Interview Questions||EJB(Enterprise JavaBeans) Interview Questions|
|JavaServer Faces (JSF) Interview Questions||Java Swing Interview Questions|
|Java 8 Interview Questions||Java XML Interview Questions|
|JavaFX Interview Questions||The Java Debugger (JDB) Interview Questions|
Java Script Related Practice Tests
|Java Script Practice Tests||Adv Java Practice Tests|
|Core Java Practice Tests||Java-Springs Practice Tests|
|Java Servlets Practice Tests||EJB(Enterprise JavaBeans) Practice Tests|
|JavaServer Faces (JSF) Practice Tests||Java 8 Practice Tests|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.