How to check the host from where Applet is loaded
Published: January 4, 2010 , Updated: August 4, 2010 , Author: mkyong
Often times, you need to know the host from where your Applet is loaded, to prevent someone else load your Applet illegally. You can determine the host from where Applet is loaded with the following simple method.
getCodeBase()
This will return an URL object, you can cast it to String for the URL checking and implement a simple copy protection of your Applet.
package com.mkyong.applet; import java.applet.*; import java.awt.Graphics; public class AppletExample extends Applet { public void init() { //get URL from where Applet is loaded String urlLoaded = getCodeBase().toString(); //check it if(urlLoaded.equals("xxx")){ //continue load it }else{ //illegal load of Applet, display something else } } }
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~