BEA Logo BEA WebLogic Server Release 5.0

  Corporate Info  |  News  |  Solutions  |  Products  |  Partners  |  Services  |  Events  |  Download  |  How To Buy

Troubleshooting CODEBASE

Contents
The CODEBASE attribute
The CODE attribute
Troubleshooting applets
Applet doesn't run in browser
ClassFormatError
Testing in your local environment
Moving from your local development environment
Speeding up applets with the ARCHIVE parameter

Useful documents
The Java Tutorial on the <APPLET> tag

The CODEBASE attribute.

You can use the CODEBASE attribute in an <APPLET> tag to specify a URL where the browser should look for the applet's Java class files. Without the CODEBASE tag, a web browser will look for the required classes under the same directory as the HTML file containing the <APPLET> tag. Using CODEBASE is convenient, since it allows you to organize your class files under a single directory, separate from the HTML content of your site.

Applets writen to work with the WebLogic Server often require WebLogic classes, so it is good practice to use the CODEBASE attribute to allow the browser to load the required classes out of the WebLogic installation. WebLogic provides a special servlet, called the ClasspathServlet, that will serve classes from the WebLogic Server's classpath. The ClasspathServlet is registered by default in the weblogic.properties file under the virtual servlet name "classes". When you set CODEBASE to a URL such as:

  CODEBASE="http://www.weblogic.com/classes/"
or
  CODEBASE="/classes/"
the WebLogic Server invokes the ClasspathServlet, which searches for the requested classes under the WebLogic Server's classpath.

When you start the WebLogic Server, the weblogic.class.path property given on the command line should include the classes/ directory, and the myserver/serverclasses/ directory, located under the root WebLogic installation. (For more details on starting the WebLogic Server, see Setting up and starting the WebLogic Server.)

You should place your applet classes under the myserver/serverclasses/ directory, so they may be served by the ClasspathServlet. This directory is pointed to by the %SERVER_CLASSES% environment variable if you have run the setEnv script to set up your development environment.

Configuring the ClasspathServlet is described in detail in the WebLogic Administrator's Guide, Setting up WebLogic as an HTTP Server.

The CODE attribute

Your <APPLET> tag must contain the CODE attribute, which specifies the full package name of the main applet classfile. The extension ".class" at the end of the CODE is optional. For example, the phonebook1 applet is declared under the package examples.applets, so would be requested in an <APPLET> tag with:

  <APPLET CODE="examples.applets.phonebook1"
          CODEBASE="/classes/" >

Here is a quick summary of how you would set up your WebLogic Server to serve the applet "c:/weblogic/classes/examples/applets/phonebook1.class" from an HTML file called "phonebook1.html". This example is taken from the examples/applets directory under the WebLogic installation.

  1. Open a command prompt and set up your environment using the setEnv script, as described in Setting up your development environment. Change to the directory containing the PhoneBook1.java source file, and compile it using:
     $  javac -d %SERVER_CLASSES% PhoneBook1.java
    This will place the compiled applet class under the myserver/serverclasses directory, where it can be served by the ClasspathServlet.

  2. Register the ClasspathServlet in the weblogic.properties file with:
    weblogic.httpd.register.classes=weblogic.servlet.ClasspathServlet
    

    This is already registered by default, but you should check that the property is not commented out.

  3. Create an HTML file that includes the applet and call it "phonebook1.html". Place this HTML file under the document root of the server. In the HTML file, add the <APPLET> tag:
      <applet code="examples.applets.PhoneBook1"
    	codebase="/classes/"
            width=500 height=800> 
        <param name=weblogic_url value="t3://localhost:7001">
        <param name=poolname value="demoPool">
      </applet>
    The <param> tags provide configuration information for this specific applet.

  4. Start (or restart) the WebLogic Server.

  5. In your browser, open the URL:
    http://WebLogicURL:Port/classes/phonebook1.html
    where WebLogicURL:Port is the URL and listen port of your WebLogic Server.

For more information on the APPLET tag and CODEBASE, see JavaSoft's Overview of Applets document, part of their Java Tutorial.

For more information on serving files with your WebLogic Server, read the WebLogic Administrators Guide document, Setting up WebLogic as an HTTP server.

Troubleshooting applets

Here are some scenarios you may run into using applets:

Applet doesn't run in browser

I'm using WebLogic JDBC in an applet to retrieve data from a DBMS. If I run the class using the Sun Appletviewer on my local machine, I have no problems. But when I try to run the applet in a Netscape browser, it will not connect.

If your applet works in Appletviewer but not in a browser, it is an indication that you are violating a Netscape security restriction; in this case, the violation is that an applet cannot open a socket to a machine other than the one from which it loaded the applet. To solve this problem, you will have to serve your applet code from the same machine that hosts the DBMS.

Note: The IP naming format you use in the applet CODEBASE and the URL you use to make a connection to the WebLogic Server must match exactly. You can't use dot-notation format in one place and domain name format in the other.

ClassFormatError

If you are getting a ClassFormatError, it probably indicates that there is a problem with your HTTP server configuration. It could be that you haven't put the WebLogic or applet classes in the correct directory on the HTTP server, or that you are specifying the CODEBASE or the CODE incorrectly in your APPLET tag. Here are some examples.

I downloaded your distribution and copied the classes to my HTTP server. I created an applet and placed it on the server in the directory /webz/ns-home/classes/applets/myapp.class. I am calling the applet with the following APPLET tag:

  <APPLET
    CODEBASE=http://myserver.com/webz/ns-home/classes
    CODE=applets.myapp
    WIDTH=500 HEIGHT=600>

This didn't work.

Your CODEBASE should point to the document root of the server. In the scenario above, the directory path you have referenced in the CODEBASE tag is an absolute directory path on the HTTP server.

Your CODEBASE should always be the URL of the server, plus the path that originates from the HTTP document root.

Solution: Place the applet in the document root of your HTTP server. If the document root is webz/ns-home/, then your APPLET tag will be:

  <APPLET
    CODEBASE="http://myserver.com/classes"
    CODE=applets.myapp
    WIDTH=500 HEIGHT=600>

If you are using the WebLogic Server as an HTTP server, there are other considerations in setting up your APPLET tag. For more information, read Setting up WebLogic as an HTTP server.

Testing in your local environment

If you are running the WebLogic Server and Netscape Communicator 4.x on the same host, you will need to remove the CLASSPATH from the environment of the shell that is running Communicator. For security reasons, Netscape Communicator will not load classes from your local CLASSPATH in order to prevent malicious changes to standard classes. Removing the local CLASSPATH when running the browser causes Netscape to load the classes from the WebLogic Server's CLASSPATH.

You will still need to set a CLASSPATH in the shell in which you start WebLogic. WebLogic recommends that you never set CLASSPATH in your environment, but rather set CLASSPATH appropriately in the shell from which you run WebLogic.

Moving from your local development environment

When you move the applet from your local environment, you will need to make sure that you install the WebLogic classes and the applet class in the proper location on the webserver.

If you are running the applet on the same machine that you installed the WebLogic distribution, this may obscure problems you are having with CODEBASE. The applet will first look for the WebLogic classes in your local CLASSPATH. If you haven't properly installed the classes for serving applets from the HTTP server, the applet will default to your local CLASSPATH and work, obscuring the problem. To test your HTTP configuration properly, you need to temporarily rename the WebLogic classes in your local CLASSPATH or try your applet from another machine.

Speeding up applets with the ARCHIVE parameter

If your applet must download many files before it can run, you can speed this up by using the ARCHIVE parameter inside the <APPLET> tag on your HTML page. A common problem with multifile applets is that the browser must make a separate HTTP connection for each file used in an applet. Making a connection can take up to several seconds, sometimes longer than downloading the file itself. With the ARCHIVE parameter you can combine these classes into a .jar file (or .cab file, for Microsoft Internet Explorer), which can then be downloaded in a single HTTP connection. Since .jar files can be compressed (.cab files are always compressed), this will also improve download time.

Note: The procedure when using Appletviewer, Netscape Navigator (3.0 and later only), and the HotJava browser differs from that used for Microsoft Internet Explorer (4.0 and later only). For complete compatibility, both methods may be combined.

Procedure for Netscape, Appletviewer, and HotJava

The ARCHIVE parameter consists of a list of archive files, separated by commas. If a path is not specified, the browser will look for the file in the same directory as the HTML file. Paths in the ARCHIVE parameter are expressed relative to the location of the HTML file. For example,
  <APPLET 
    CODEBASE="http://myserver.com/classes"
    CODE=myFile.class 
    ARCHIVE="myArchive.jar,jarfiles/myArchive2.jar"
    WIDTH=500 HEIGHT=600> 
    </APPLET>

will specify that the browser look first in the same directory as the HTML page for myArchive.jar and then in the "jarfiles" directory below the current directory for myArchive2.jar. If the browser cannot locate the required files in the .jar file, it will look in the location specified by the CODEBASE parameter.

Procedure for Microsoft Internet Explorer

Since Microsoft Internet Explorer will not recognize .jar files, you must create .cab files instead. The use of the <APPLET> tag is also different in that you will need to specify the .cab file as a <PARAM> rather than specifying it within the <APPLET> tag. For example,
<APPLET 
  CODEBASE="http://myserver.com/classes"
  CODE="myfile.class"
  WIDTH=500
  HEIGHT=600>

 <PARAM NAME=cabbase VALUE="myArchive.cab">

 </APPLET>

Combining both .cab and .jar files

If you need to support Microsoft Internet Explorer as well as the other browsers mentioned above, you can combine the methods by creating both .cab and .jar archives of your files, and specifying them as follows:
<APPLET 
  CODEBASE="http://myserver.com/classes"
  CODE="myfile.class"
  ARCHIVE="myArchive.jar,jarfiles/myArchive2.jar"
  WIDTH=500
  HEIGHT=600>

 <PARAM NAME=cabbase VALUE="myArchive.cab">

 </APPLET>
For information on creating archives, see Using the AppletArchiver to create a .jar or .cab archive

 

Copyright © 2000 BEA Systems, Inc. All rights reserved.
Required browser: Netscape 4.0 or higher, or Microsoft Internet Explorer 4.0 or higher.
Last updated 01/14/1999