Question

How can I load a particular HTML page from within an applet?

Answer

Applets are executed within a web browser, and there's an easy way to load show a specific URL.

  1. Obtain a reference to the applet context
  2. Call the showDocument method, which takes as a parameter a URL object.

The following code snippet shows you how this can be done.

import java.net.*;
import java.awt.*;
import java.applet.*;

public class MyApplet extends Applet
{
	// Your applet code goes here

	// Show me a page
	public void showPage ( String mypage )
	{
		URL myurl = null;

		// Create a URL object
		try
		{
			myurl = new URL ( mypage );
		}
		catch (MalformedURLException e)
		{
			// Invalid URL
		}
			
		// Show URL
		if (myurl != null)
		{
			getAppletContext().showDocument (myurl);
		}

	}

}


Back