Applet 在从 Eclipse 运行时可以正常工作,但在浏览器中则不太好
我有一个与 Java 小程序相关的问题,非常感谢您的帮助。故事是这样的: 我尝试创建一个小程序,它会尝试连接到 digg.com 并通过使用它的 API,显示 100 个最受欢迎的故事(还包括一些对结果进行排序和过滤的选项)。
不管怎样,小程序打开并在浏览器中很好地显示 GUI,但是当我按下加载新闻的按钮时,它不会加载它们,但当我在 Eclipse 中运行小程序时,新闻已加载。
我想,也许问题与从 eclipse 创建可运行的 jar 有关,因为我有一些引用的库(用于处理来自 digg.com 的 JSON 的 Google GSON)。以下是我用于创建可运行 jar 的设置(我还尝试了“将所需的库打包到生成的 JAR 中):
http://img816.imageshack.us/img816/417/exportjar.png
这是新闻加载后小程序的图片(从 eclipse 运行): http://img841.imageshack.us/img841/9121/applet.png
对于如何在网络浏览器中加载新闻的任何想法,我将不胜感激。
I have a Java applet related problem and I would appreciate your help. Here's the story:
I tried creating an applet, that would try to connect to digg.com and by using it's API, show the 100 most popular stories (also include some options to sort and filter the results).
Anyway, the applet opens and shows the GUI just fine in the browser, but when I press the button that loads the news, it won't load them, BUT the news are loaded, when I run the applet in Eclipse.
I thought, that maybe the problem is related to creating the runnable jar from eclipse, since I have some referenced libraries (Google GSON for handling JSON from digg.com). Here are the settings I used for creating the runnable jar (I also tried "Package required libraries into generated JAR):
http://img816.imageshack.us/img816/417/exportjar.png
Here's a picture of the applet (ran from eclipse), when the news have been loaded:
http://img841.imageshack.us/img841/9121/applet.png
I would appreciate any ideas on how to get loading the news to work in the web browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为托拉是对的。小程序应该只能连接到它们自己的服务器。您可以尝试欺骗浏览器,将请求发送回服务器,要求其检索数据并将其返回给小程序。
I think torah is right. Applets should be able to connect only to their own server. You can try and trick the browser, sending request back to your server asking it to retrieve the data and to return it to applet.
当您尝试打开与 digg 的连接时会发生什么?是否有安全例外?如果得知浏览器内的 JVM 正在实施同源策略,我不会感到惊讶。如果是这种情况,当您尝试连接到 digg.com 时,应该会引发某种异常(我认为是 SecurityException)。
快速搜索“同源策略 java”发现了以下文章,这两篇文章似乎都证实这可能是您的问题。
http://www.zdnet。 com/blog/security/defeating-the-same-origin-policy-part-1/946
What happens when you attempt to open the connection to digg? Is there a security exception? I wouldn't be surprised to learn that the JVM inside the browser was implementing a same origin policy. If this is the case there should be some kind of Exception (SecurityException I think) being raised when you try to connect to digg.com.
A quick search for "same origin policy java" found the following article which both seems to confirm that this could be your problem.
http://www.zdnet.com/blog/security/defeating-the-same-origin-policy-part-1/946