如何获取当前运行 Java Applet 的网页的 URL? [英] How do I get the URL of the web page a Java Applet is currently running on?

查看:33
本文介绍了如何获取当前运行 Java Applet 的网页的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 制作一个小程序,它将嵌入到网页中,但页面的 URL 是动态的.我需要能够获取运行小程序的页面的当前 URL.有人知道怎么做吗?

I am making an applet in Java that will be embedded into a web page, but the URL of the page is dynamic. I need to be able to get the current URL of the page that the applet is running on. Does anyone know how to do this?

推荐答案

参见 getDocumentBase()

使用 getDocumentBase() 实际上给了我一个 NullPointerException..

Using getDocumentBase() actually gave me a NullPointerException..

在这里工作正常.我刚刚使用此代码对其进行了测试.

It works fine here. I just tested it using this code.

// <applet code=MyLocation width=400 height=30></applet>
import javax.swing.*;

public class MyLocation extends JApplet {

    @Override
    public void init() {
        add( new JLabel(getDocumentBase().toString()));
    }
}

编译&运行它,保存源代码后执行以下操作.

To compile & run it, do the following after saving the source.

prompt> javac MyLocation.java
prompt> appletviewer MyLocation.java

第二行的 .java 扩展名没有错别字.最新版本的小程序查看器将在指定的源代码中查找注释中定义的 applet 元素.

The .java extension on the second line is no typo. Recent versions of applet viewer will look in the specified source code for an applet element defined in a comment.

这篇关于如何获取当前运行 Java Applet 的网页的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆