by Dinesh Thakur Category: AWT and Applets

In most of the applets, it is required to load text and images explicitly. Java enables loading data from two directories. The first one is the directory which contains the HTML file that started the applet (known as the document base). The other one is the directory from which the class file of the applet is loaded (known as the code base). These directories can be obtained as URL objects by using getDocumentBase ()and getCodeBase ()methods respectively. You can concatenate these URL objects with the string representing the name of the file that is to be loaded.

Here is the java code for the program GetDocumentBase and getCodeBase Example :.*/

 

/*
    <applet code="GetDocumentBaseExample" width=350 height=250>
    </applet>
    */
 
   import java.applet.Applet;
    import java.awt.Graphics;
    import java.net.URL;
    import java.awt.*;
    import java.awt.event.*;
     
    public class GetDocumentBaseExample extends Applet{
     
            public void paint(Graphics g){
            String message;
          
        //getCodeBase() method gets the base URL of the directory in which contains this applet.
         URL appletCodeDir=getCodeBase();
          message = "Code Base : "+appletCodeDir.toString();
          g.drawString(message,10,90);
         
         // getDocumentBase() Returns an absolute URL of the Document
          URL appletDocDir = getDocumentBase();        
          message="Document Base : "+appletDocDir.toString();
          g.drawString(message,10,120);
          g.drawString("http://ecomputernotes.com", 200, 250);
                  
                   
            }
    }

GetDocumentBase and getCodeBase Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles