How to redirect to a new URL in Applet
Published: January 5, 2010 , Updated: January 4, 2010 , Author: mkyong
This Applet’s code snippet will open a new browser’s tab and go to the Google website.
getAppletContext().showDocument(new URL("http://www.google.com"), "_blank");
You also can redirect user to another page within the same domain
getAppletContext().showDocument(new URL(getCodeBase()+"newpage.html"),"_top");}
Example
This Applet’s example will redirect user to a new URL while the Applet is initialized.
package com.mkyong.applet; import java.applet.*; import java.awt.Graphics; import java.net.MalformedURLException; import java.net.URL; public class AppletExample extends Applet { public void init() { try { getAppletContext().showDocument(new URL("http://www.google.com"), "_blank"); } catch (MalformedURLException ex) { System.out.println(ex.getMessage()); } } public void paint( Graphics g ) { g.drawString("Go Google", 0,100); } }
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~