In this article, we will show you how to make an applet(running in a webpage) open a new webpage in the new tab of the browser. This will be accomplished by
showDocument() method of AppletContext interface. AppletContext interface gives us the access to the environment in which an applet is running and through this enviroment we can open a new webpage in the web browser.
Signature of showDocument() method
void showDocument(URL url, String str)
where, url is the location of the new webpage that we want to open from our applet and String value str describes where to display this new webpage.
str could take a few values like -
_blank, opens the new webpage in a new tab of web browser.
_self, opens the new webpage in the current tab of web browser that contains the running applet.
Note -:
Due to new security restrictions imposed by many browsers, applets aren't allowed to run in Google Chrome, Mozilla Firefox, though we can still
run applets within a webpage in Internet Explorer
but only after making certain changes in the configuration of Java on our system. So, this example is based on opening an applet in a webpage within the Internet Explorer browser.
Points to remember before running an applet from within a webpage.
Both webpage and the applet code must be in the same folder.
In our example, both the webpage - Sample.htm and the applet - Applet15.java byteclass file Applet15.class are within the same
D:/Java folder.
Applet15.java
import java.awt.*;
import java.applet.*;
import java.net.*;
public class Applet15 extends Applet
{
AppletContext aCon;
public void start()
{
System.out.println("initializing an applet");
aCon = getAppletContext();
URL url = getCodeBase();
try
{
aco.showDocument(new URL(url+"Sample.htm"), "_blank"); //Applet will open a Hello.htm webpage in new blank tab of the browser
}
catch(MalformedURLException mue)
{
System.out.println("URL not found");
}
}
public void paint(Graphics g)
{
setBackground(Color.black);
setForeground(Color.blue);
g.drawString("Applet showing another applet", 0,20);
g.drawString("The full path to our applet file : " + getDocumentBase(), 0, 40); //getDocumentBase() gives URL object
g.drawString("Directory in which our applet file is located : " + getCodeBase(), 0, 60);//getCodeBase() gives URL object
}
}
After compiling the above mentioned code Applet15.java, we get its bytecode fine - Applet15.class
The name of our webpage which contains the <applet> tag is Sample.htm. This webpage will load the compiled bytecode "Applet15.class" file using <applet> tag in it.
Let's see the contents of Sample.htm Sample.htm
<html>
<body>
<applet code="Applet15" width=400 height=400>
</applet>
Hello there, let's open another webpage from this applet.
</body>
</html>
Advertisement
How to run applet - Applet15 from within the webpage - Sample.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 Sample.htm webpage file to the exception list in "Configure Java" settings,
this will allow Sample.htm webpage to run the applet(Applet15) mentioned in it, in 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, Sample.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 contained in Sample.htm opens up and it also opens up another webpage Hello.htm in a new blank tab of browser.
Sample.htm
Hello.htm
As you may see, an applet Applet15 linked to a webpage Sample.htm has opened another webpageHello.htm