How to redirect to a new URL in Applet

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);
	}
   
}

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
shops Recent comment authors
newest oldest most voted
shops
Guest
shops

Oh my goodness! an incredible article dude. Thank you However I’m experiencing situation with ur rss . Don? know why Unable to subscribe to it. Is there anybody getting an identical rss downside? Anybody who is aware of kindly respond. Thnkx