我如何让网页的Java小程序当前正在运行的网址是什么? [英] How do I get the URL of the web page a Java Applet is currently running on?
问题描述
我正在用Java小应用程序将被嵌入到一个网页,但网页的网址是动态的。我需要能够得到的小应用程序上运行的网页的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..
它工作在这里很好。我只是用这个code测试它。
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浏览器的最新版本将查找指定的源$ C $ C为小程序在评论中定义
元素。
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小程序当前正在运行的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!