一起托管 php 和 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13897290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hosting php and Java together
提问by Hyman
I am new to web development, bear with me, the problem is that I am writing a web application that is mostly written in PHP. Although half the code has already been written in Java by a different developer, my hosting server doesn't support java. Can anyone help me with what my options are, I have googled and I've seen a php to java bridge as a possible solution. The thing is that I can't do that because I can't currently host java servlets. If that is my only option I will have to use a different hosting company, though it is not my preferred solution.
我是 Web 开发的新手,请耐心等待,问题是我正在编写一个主要用 PHP 编写的 Web 应用程序。虽然一半的代码已经由不同的开发人员用 Java 编写,但我的托管服务器不支持 java。任何人都可以帮助我了解我的选择,我已经用谷歌搜索过,我已经看到一个 php 到 java 桥作为可能的解决方案。问题是我不能这样做,因为我目前无法托管 Java servlet。如果这是我唯一的选择,我将不得不使用不同的托管公司,尽管这不是我的首选解决方案。
I don't know Java - is it possible to compile the java files into .class files and reference it from my php code, could my Apache server host the java .class files.
我不知道 Java - 是否可以将 java 文件编译成 .class 文件并从我的 php 代码中引用它,我的 Apache 服务器是否可以托管 java .class 文件。
In short I need to host the Java and php code as well as reference the Java code from my php code, can I do this or would I have to rewrite the Java code in php for this to work.
简而言之,我需要托管 Java 和 php 代码以及从我的 php 代码中引用 Java 代码,我可以这样做还是必须在 php 中重写 Java 代码才能使其工作。
回答by paulsm4
1) Most web hosting sites offer someprovision for JSP/Servlets (e.g. Tomcat). Even the least expensive web site hosting services. So looking for another provider isn't necessarily a bad idea.
1) 大多数网络托管站点都为 JSP/Servlets(例如 Tomcat)提供了一些规定。即使是最便宜的网站托管服务。因此,寻找其他供应商不一定是个坏主意。
2) Java code can't talk directly to PHP code, and PHP code can't directly talk to Java code.
2)Java代码不能直接和PHP代码对话,PHP代码也不能直接和Java代码对话。
3) HOWEVER: the same web server can be configured to run both PHP/Apache and Tomcat. For example:
3) 但是:可以配置同一个 Web 服务器来运行 PHP/Apache 和 Tomcat。例如:
4) The same servercan easilysupport both PHP/Apache and Tomcat as separate services. In other words, the same host (e.g. www.myserver.com) can easily run Apache/PHP (e.g. http://www.myserver.com:80) and Tomcat (e.g. http://www.myserver.com:8080) concurrently. This is a very common configuration.
4) 同一个服务器可以很容易地同时支持 PHP/Apache 和 Tomcat 作为单独的服务。换句话说,同一个主机(例如 www.myserver.com)可以轻松运行 Apache/PHP(例如http://www.myserver.com:80)和 Tomcat(例如http://www.myserver.com:8080) 同时。这是一个很常见的配置。
5) Do notlook at any "translators". Even if you find a tool that claims to be able to do it, converting from PHP to Java, or Java to PHP, is probably going to me more trouble than it's worth.
5)不要不看任何“翻译”。即使您找到了声称能够做到这一点的工具,从 PHP 转换为 Java,或从 Java 转换为 PHP,也可能会给我带来更多的麻烦。
6) Unless there's a lotof code, I wouldn't really consider Web services (or Ajax) either.
6) 除非有很多代码,否则我也不会真正考虑 Web 服务(或 Ajax)。
7) Just find a provider who supports Apache/PHP, Tomcat and your database of choice; create the "main page" on whichever one you feel most comfortable with, and modify your PHP and your JSP pages to rely on CSS to insure both have a consistent look and feel.
7) 只需找到一个支持 Apache/PHP、Tomcat 和您选择的数据库的提供商;在您感觉最舒服的任何一个上创建“主页”,并修改您的 PHP 和 JSP 页面以依赖 CSS 以确保两者具有一致的外观和感觉。
This approach is the easiest ... and it mightjust be all you need!
这种方法是最简单的……它可能正是您所需要的!
Good luck
祝你好运
回答by Igwe Kalu
You don't need any bridge. But you may use the Java app as a service... Simply make a request to it from your PHP (using an AJAX call possibly) and then display the response as it was returned.
你不需要任何桥梁。但是您可以将 Java 应用程序用作服务...只需从您的 PHP 向它发出请求(可能使用 AJAX 调用),然后在返回时显示响应。
That's the simplest solution I can offer.
这是我能提供的最简单的解决方案。
Before you ask more question I suggest you read a bit about AJAX hereand web service here. :-)
之前你问更多的问题,我建议你读一些关于AJAX在这里和Web服务在这里。:-)
jQuery Ajax has the dataType property by which you can clearly specify how you wish to receive a response, and HTML is one of the options. But he doesn't even have to tweak that since Ajax can intelligently guess the nature of the data and return it as expected. The only issue that may exist in this method is that it may introduce some latency.
jQuery Ajax 具有 dataType 属性,您可以通过该属性明确指定您希望如何接收响应,HTML 是其中一种选项。但他甚至不必调整它,因为 Ajax 可以智能地猜测数据的性质并按预期返回它。这种方法可能存在的唯一问题是它可能会引入一些延迟。
You can find out about how you can use jQuery/Ajax to solve this problem by reading this docand read about the options here.
您可以通过阅读此文档并阅读此处的选项,了解如何使用 jQuery/Ajax 解决此问题。
Read my comments on the other answers too, if you will...
也请阅读我对其他答案的评论,如果您愿意...
回答by Ondra ?i?ka
Regarding (even free) Java and PHP hosting, try Red Hat's OpenShift
关于(甚至免费)Java 和 PHP 托管,请尝试Red Hat 的 OpenShift
Regarding PHP interacting with Java and vice versa - there's multitude of ways:
关于 PHP 与 Java 交互,反之亦然 - 有多种方法:
- XMLRPC
- REST - see Can you use REST in PHP? If so how?and e.g. RestEasy http://www.jboss.org/resteasy
- PHP's Java integration - http://php.net/manual/en/book.java.php
etc.
- XMLRPC
- REST - 请参阅您能否在 PHP 中使用 REST?如果是这样怎么办?和例如 RestEasy http://www.jboss.org/resteasy
- PHP 的 Java 集成 - http://php.net/manual/en/book.java.php
等。
Also, there was Caucho Quercus, PHP interpreted in Java. But that's a dead project AFAIK
此外,还有Caucho Quercus,用 Java 解释的 PHP。但这是一个死的项目 AFAIK
回答by Nam Nguyen
Without knowing how your Java and PHP are written, AJAX may be your best bet. But it may be possible to use Quercus to both execute your PHP code and call into your Java routines, all on the same Java server. Of course, that will require running on a server that supports servlets.
在不知道您的 Java 和 PHP 是如何编写的情况下,AJAX 可能是您最好的选择。但是可以使用 Quercus 执行 PHP 代码并调用 Java 例程,所有这些都在同一个 Java 服务器上。当然,这需要在支持 servlet 的服务器上运行。
@Ondra ?i?ka, Quercus is alive and well, thank you very much :).
@Ondra ?i?ka,Quercus 还活着,很好,非常感谢:)。
回答by vishal
Recently we used mod_jk for Apache(for PHP) and JBOSS for JAVA. So, base server will be Apache and from where request redirect to JBOSS. This might be helpful for you
最近我们使用 mod_jk 用于 Apache(用于 PHP)和 JBOSS 用于 JAVA。因此,基本服务器将是 Apache,并且请求从哪里重定向到 JBOSS。 这可能对你有帮助