get DocumnetBase() and get CodeBase() , show Document ()

Java-GetCodeBase and GetDocumentBase

GetCodeBase and GetDocumentBase
You will create applets that will need to explicitly  load media and text. Java will allow the applet to load data from the directory holding the html file that started the applet (the document base) and the directory from which the applet’s class file was loaded (the code base). These directories are returned by  getDocumentBase( ) and getCodeBase( ).

Program Source
import java.applet.Applet;
import java.awt.Graphics;

public class Javaapp extends Applet{
   
    public void paint(Graphics g)
    {
        g.drawString("getCodeBase     : "+getCodeBase(), 20, 20); 
        g.drawString("getDocumentBase : "+getDocumentBase(), 20, 40);
    }
}

showDocument () 
Java allows the applet to transfer the control to another URL by using the showDocument () Method defined in the AppletContext interface. For this, first of all, it is needed to obtain the Context of the currently executing applet by calling the getAppletContext () method defined by the Applet. Once the context of the applet is obtained with in an applet, another document can be brought into view by calling showDocument () method.
There are two showDocument () methods which are as follows:
showDocument(URL url)
showDocument(URL url,string lac)
where,
url is the URL from where the document is to be brought into view.
loc is the location within the browser window where the specified document is to be displayed.
Example An applet code to demonstrate the use of AppletContext and showDocument ().
import java.applet.Applet;
import java.applet.AppletContext;
import java.net.*;
/*
<applet code="LoadHTMLFileSample" width="700" height="500"></applet>
*/
public class LoadHTMLFileSample extends Applet
{
    public void start()
    {
          AppletContext context= getAppletContext();
          //get AppletContext
          URL codeBase = getCodeBase(); //get Applet code base
          try{
                 URL url = new URL(codeBase + "Test.html");
                 context.showDocument(url,"_blank");
                 repaint();
               }catch(MalformedURLException mfe) {
                         mfe.printStackTrace();
                         }
     }
}

No comments:

Post a Comment