简体   繁体   中英

Javascript_Java_Interaction applet “Codebase” problem

I saw an article about Javascript_Java_Interaction today at :

 http://www.rgagnon.com/javadetails/java-0184.html
 [ You can try the working version on that site ]
 So I tried it on my PC, after some simple format change the files look like this :

============================================================================================

[C:/Dir_Fit/Javascript_Java_Interaction.html]

    <FRAMESET ROWS="100%,*">
      <FRAME NAME="mainFrame" SRC="Javascript_Java_Interaction_Visible_Page.html" border=0>
      <FRAME NAME="scriptFrame" SRC="Javascript_Java_Interaction_Invisible_Page.html" border=0>
    </FRAMESET>
--------------------------------------------------------------------------------------------

[C:/Dir_Fit/Javascript_Java_Interaction_Invisible_Page.html]

<HTML>
  <HEAD>
    <SCRIPT>
      function replace(s,t,u)
      {
        i=s.indexOf(t);
        r="";
        if (i==-1) return s;
        r+=s.substring(0,i)+u;
        if (i+t.length<s.length) r+=replace(s.substring(i+t.length,s.length),t,u);
        return r;
      }

      function getAndSendMessage()
      {
        theMessage=document.location.search.substring(1,255)
        if (theMessage.length>0)
        {
          // replace all '+" by space
          theMessage=replace(theMessage,'+',' ')
          window.parent.mainFrame.showMessage(unescape(theMessage))
        }
      }
    </SCRIPT>
  </HEAD>
  <BODY onLoad="getAndSendMessage();"></BODY>
</HTML>
-----------------------------------------------------------------------------------------
[C:/Dir_Fit/Javascript_Java_Interaction_Visible_Page.html]

<HTML>
  <HEAD><SCRIPT>function showMessage(s) { alert(s) }</SCRIPT></HEAD>
  <BODY>
    <H1>Simple Java - Javascript interaction</H1><P>
    <APPLET CODEBASE="file://C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>
  </BODY>
</HTML>
-----------------------------------------------------------------------------------------
Source : [C:/Dir_Fit/src/Javascript_Java_Interaction_Demo.java]
class  : [C:/Dir_Fit/build/classes/Javascript_Java_Interaction_Demo.class]

import java.applet.Applet;
import java.awt.*;

public class Javascript_Java_Interaction_Demo extends Applet
{
  TextField aMessage;
  Button sendButton;

  public void init()
  {
    aMessage=new TextField(20);
    add(aMessage);
    sendButton=new Button("Send to Javascript");
    add(sendButton);
  }

  public boolean action(Event e,Object o)
  {
    if (e.target.equals(sendButton))
    {
      try { getAppletContext().showDocument(new java.net.URL(getCodeBase(),"Javascript_Java_Interaction_Invisible_Page.html?"+java.net.URLEncoder.encode(aMessage.getText())),"scriptFrame"); }
      catch (Exception ex) { ex.printStackTrace(); }
    }
    return true;
  }
}

==========================================================================================

It displayed the applet on the page [Javascript_Java_Interaction.html], but if I type in some text and click the button, nothing happens, the problem is in the following line :

<APPLET CODEBASE="file://C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>

So I copied the class file into the same dir as the html files [C:/Dir_Fit/] and changed the above line to :

<APPLET CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>

now it works, it will display an alert window.

So, my question is why it didn't work when I specified the codebase ? According to Java document if the class file is in a different dir, you can tell it by codebase, the applet will show up but if you type in some text and click the button, nothing will happen, I've tried different ways to specify the code base :

    <APPLET CODEBASE="file:///C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>

    <APPLET CODEBASE="C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>

    <APPLET CODEBASE="build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>

<APPLET CODE="C:/Dir_Fit/build/classes/Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>

Yet, none of them works, why ? I don't want the class file to be in the same dir as the htmls. What's the fix ?

Frank

It might have something to do with the changes made in Java 6 Update 10. After the release my applets started to fail with ClassNotFoundExceptions independent of the browser - strangely they worked with 6u7 before that. First I thought about a messed up installment or configuration but then others have experienced the same thing in their system.

My trial-and-error came up with the very same result as your observation, namely if I specify even an empty codebase attribute, my applet fails.

Googling around (today) does not come up with any meaningful results or bug reports issued. I would guess the rules for the codebase attribute usage changed between versions - maybe the old one wasn't restrictive enough or wasn't conformant to some specification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM