In this article, we will show you how to open an applet in a webpage within a web browser.
Note : Due to new security restrictions imposed by many browsers, applets aren't allowed to run in Google Chrome, Mozilla Firefox browsers, though we can still
run applets within a webpage within in the Microsoft Internet Explorer browser, but, only after making certain changes in the configuration of Java on our system.
The upcoming applet code will display an applet in a webpage within the Internet Explorer browser.
But remember, this applet won't run in Google Chrome or Firefox Mozzilla web browsers.
Points to remember before running an applet in a webpage.
Both webpage and the applet bytecode(.class) must be in the same folder.
In our example, the webpage - A.htm and our the applet bytecode file Applet7.class are within the same
D:/Java folder.
Applet7.java
import java.awt.*;
import java.applet.*;
public class Applet7 extends Applet
{
public void init()
{
System.out.println("initializing an applet");
}
public void paint(Graphics g)
{
setBackground(Color.cyan);
System.out.println("Hello there");
g.drawString("Drawing an oval, rectangle and line in our applet", 0,10);
g.drawOval(50,50,100,100);
g.drawRect(150,150,100,100);
g.drawLine(270,270,350,350);
}
}
After compiling the above mentioned code Applet7.java, we get its bytecode fine - Applet7.class
The name of our webpage which contains the <applet> tag is A.htm. This webpage loads the compiled bytecode Applet7.class file using <applet> tag in it.
Let's see the contents of A.htm file -
A.htm
<html>
<body>
<applet code="Applet7" width=400 height=400>
</applet>
Opening an applet within a webpage.
</body>
</html>
Advertisement
How to run our applet - "Applet7" from within webpage A.htm ?
Click on Start ->All Programs -> Java-> Configure Java. This will open dialog box of Java Control Panel. We need to click on its Security tab.
In the Security tab click on button Edit Site List to add the full path of to A.htm webpage file to the Exception Site List,
this will allow A.htm webpage to run the applet-Applet7 mentioned in it, within the Internet Explorer browser.
Note : When you are adding a webpage to the exception list, make sure your path to folder containing your webpage must begin with
file:///
As soon as you add the path to the exception list, click on the Add button to have the path show up in the exception list and click on OK to close the dialog box.
Now, open the webpage, A.htm in the Internet Explorer, you will see a window similar to :
As soon as you click on the button "Allow blocked content", your applet Applet7 contained in webpage A.htm is executed within this webpage.
A.htm with our applet running in it.
As you may see, our applet named Applet7 is being running in a webpage A.htm within the Internet Explorer browser.