简体   繁体   English

Javascript_Java_Interaction小程序“代码库”问题

[英]Javascript_Java_Interaction applet “Codebase” problem

I saw an article about Javascript_Java_Interaction today at : 我今天在看到有关Javascript_Java_Interaction的文章:

 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 : 它在页面[Javascript_Java_Interaction.html]上显示了applet,但是如果我输入一些文本并单击按钮,则什么也没有发生,问题出在以下行:

<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 : 所以我将类文件复制到与html文件[C:/ Dir_Fit /]相同的目录中,并将上面的行更改为:

<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 : 根据Java文档,如果类文件位于不同的目录中,则可以通过代码库告诉它,该applet将会显示,但是如果您键入一些文本并单击该按钮,则什么也不会发生,我尝试了不同的方法来指定代码库:

    <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. 我不希望类文件与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. 它可能与Java 6 Update 10中所做的更改有关。发布之后,我的小程序开始失败,出现ClassNotFoundExceptions独立于浏览器的情况-奇怪的是,在此之前,他们使用6u7。 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. 我的反复试验得出的结果与您的观察结果完全相同,即,即使我指定了一个空的codebase属性,我的applet也会失败。

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. 我猜想代码库属性用法的规则在版本之间会有所变化-也许旧的规则不够严格,或者不符合某些规范。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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