简体   繁体   English

在Java应用程序中侦听自定义协议

[英]Listen for custom protocol in a java application

I have created a java app and I would like to be able to execute actions in this app by calling some custom urls (ex: myapp://do_this) 我已经创建了一个Java应用程序,并且希望能够通过调用一些自定义网址(例如:myapp:// do_this)在此应用程序中执行操作

I have already searched for this, and I have found some information about handling such urls in java (URLStreamHandler). 我已经进行了搜索,并且找到了一些有关在Java(URLStreamHandler)中处理此类url的信息。

The only part I'm missing, is how to tell the OS to redirect the "myapp://" protocol to my application. 我唯一缺少的部分是如何告诉操作系统将“ myapp://”协议重定向到我的应用程序。

More and more apps are defining their own protocol and I was wondering whether it was possible to create such kind of things in Java. 越来越多的应用程序正在定义自己的协议,我想知道是否有可能在Java中创建这种东西。

Thanks 谢谢

The protocol in URLStreamHandler will be used inside of the jvm. URLStreamHandler中的协议将在jvm内部使用。 Typical usage is something like res://... for resources. 资源的典型用法类似于res://...。 The OS and the browsers have their own sets of protocols (ftp, mailto). 操作系统和浏览器具有自己的协议集(ftp,mailto)。

You could probably extend Firefox with a plugin to handle your protocol. 您可能可以使用插件扩展Firefox以处理您的协议。

But then you have to send that to your app, running as a small "web" server. 但是随后,您必须将其发送到作为小型“网络”服务器运行的应用程序。

Forget for a moment URLStreamHandler. 暂时忘掉URLStreamHandler。

Embed the Jetty web server in your app, say on port 8765, and write a servlet to handle your URLs. 将Jetty Web服务器嵌入到您的应用程序中,例如在端口8765上,并编写一个servlet来处理您的URL。 Then you can in your browser type "http://localhost:8765/do_this". 然后,您可以在浏览器中输入“ http:// localhost:8765 / do_this”。 Should suffice. 应该足够了。

To add a new protocol, one needs to implement an XPCOM component. 要添加新协议,需要实现一个XPCOM组件。 Since XPCOM allows programming languages to talk to eachother, XPCOM components can be implemented in C++ or JavaScript currently in Mozilla. 由于XPCOM允许编程语言相互交流,因此XPCOM组件可以用Mozilla当前使用的C ++或JavaScript实现。

http://www.nexgenmedia.net/docs/protocol/ will help you to understand more. http://www.nexgenmedia.net/docs/protocol/将帮助您了解更多信息。

i think this is what you want. 我想这就是你想要的。

As others have said, getting browsers to understand a new protocol name is browser (and OS) specific - you can't do it from the server. 正如其他人所说,让浏览器了解新的协议名称是特定于浏览器(和OS)的-您不能在服务器上做到这一点。

However, would Java Web Start ( http://download.oracle.com/javase/tutorial/deployment/webstart/ ) fit your requirements? 但是,Java Web Start( http://download.oracle.com/javase/tutorial/deployment/webstart/ )是否适合您的要求? Most browsers are already set up to handle JWS applications correctly. 大多数浏览器已经设置为正确处理JWS应用程序。

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

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