4

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.

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.

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.

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
jack
  • 59
  • 1
  • 1
  • 3
  • And on the subject of interacting with whatever parts of the app are *completed* in Java, see [Guzzle](http://guzzlephp.org/). This also is built to easily integrate into Symfony. Find more out about [Sonata](https://github.com/sonata-project/) and [KnpBundles](http://knpbundles.com/). Don't reinvent the wheel. – Jared Farrish Dec 16 '12 at 01:03
  • Having half of app in Java and switching to PHP? That's like having half-built Ferarri and towing it with Wartburg. – Ondra Žižka Dec 16 '12 at 07:42

6 Answers6

5

1) Most web hosting sites offer some provision 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.

2) Java code can't talk directly to PHP code, and PHP code can't directly talk to Java code.

3) HOWEVER: the same web server can be configured to run both PHP/Apache and Tomcat. For example:

4) The same server can easily support 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.

5) Do not look 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.

6) Unless there's a lot of code, I wouldn't really consider Web services (or Ajax) either.

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.

This approach is the easiest ... and it might just be all you need!

Good luck

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • That's a great point about the "code conversion" vaporware. Didn't Microsoft have a tool they released a while back that "converted" Java to C#. Everyone got all excited, then it came out; they pulled up the "converted" files, whole and massive chunks of code had been wholesale commented out? With the development and proliferation of transport systems and layers for data, you just work out a REST service and all the PHP and Rube Rails geeks are getting all in your business. We have a JIRA system and I had *VBA* grabbing data for reports. – Jared Farrish Dec 16 '12 at 07:53
  • You've only just suggested how to go about getting support for Apache and Tomcat, but nothing you said answers the question on how to create a channel of interaction between both systems. The reason why I suggested AJAX is this: there's no need for PHP to do the job that Java has done already. So, simply ask Java to give you a copy of the page it would have rendered if called directly by a user-agent, get it as a response and render it too. Finished - it's that easy. – Igwe Kalu Dec 16 '12 at 12:23
  • 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. – Igwe Kalu Dec 16 '12 at 12:29
  • @paulsm4 Thank you for your answers, definitely clarified a couple of issues I had, I had no idea that there were hosting companies currently supporting both Apache/PHP and Tomcat I am currently with Go Daddy - from what I can tell they have stopped Java hosting, thanks again for your time in answering, it is really appreciated. – jack Dec 18 '12 at 17:27
  • I might be trying 3) soon myself... Has anyone had success so far with it? – cellepo Aug 04 '15 at 00:36
0

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.

That's the simplest solution I can offer.

Before you ask more question I suggest you read a bit about AJAX here and web service here. :-)

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.

You can find out about how you can use jQuery/Ajax to solve this problem by reading this doc and read about the options here.

Read my comments on the other answers too, if you will...

Igwe Kalu
  • 14,286
  • 2
  • 29
  • 39
  • Thank you very much for your answers, as I am a newbie I will look for a hosting company that will host both Apache/PHP and Tomcat. Thanks again for your time in answering and the resource links you provided I will look at those also for the integration part – jack Dec 18 '12 at 17:44
0

Regarding (even free) Java and PHP hosting, try Red Hat's OpenShift

Regarding PHP interacting with Java and vice versa - there's multitude of ways:

Also, there was Caucho Quercus, PHP interpreted in Java. But that's a dead project AFAIK

Community
  • 1
  • 1
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • Thank you @Ondra Žižka, the Red Hat's OpenShift project is interesting I will look in to that, thanks again for your time in answering, it is very appreciated. – jack Dec 18 '12 at 17:19
  • You can say all that by clicking on the up arrow, saves time ;-) – Ondra Žižka Dec 18 '12 at 18:16
0

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.

@Ondra Žižka, Quercus is alive and well, thank you very much :).

Nam Nguyen
  • 36
  • 2
0

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

Community
  • 1
  • 1
vishal
  • 685
  • 2
  • 6
  • 9
0

mod_php can be other solution if php and java working on same servers/hostings FastCGI if php and java working on different machines(servers or hostings)

anyway(do not matter which method will use!) is good as practice to use APC or other caching(PoC) to improve the site's speed

Ion Stefanache
  • 147
  • 1
  • 4