|
From: <php...@li...> - 2009-08-17 16:45:29
|
Hi Massimo,
The servlet can handle POST, but I haven't tried to open the input stream.
However, if I understand you correctly, you want to invoke PHP application
methods from a java desktop application. Sure, you could use json or even
invent your own text-based communication protocol, but why not use the php
java bridge to handle the communication?
Here's a simple php script which acts as a receiver. It waits for java
requests and invokes methods from the closed-over environment:
<?php require_once("java/Java.inc");
class MyClass {
function hello() { return "hello"; }
function world() { return "world"; }
}
if (java_getHeader("X_JAVABRIDGE_INCLUDE", $_SERVER))
java_context()->call(java_closure(new MyClass()));
?>
And this is the Java part, which uses the jsr223 API to call the remote
methods:
import java.io.*;
import java.net.*;
import javax.script.*;
public class JsrTest {
public static void main(String s[]) throws Exception {
ScriptEngine e = new ScriptEngineManager().getEngineByName("php-invocable");
e.eval (new php.java.script.URLReader(new URL("http://localhost/jsrtest.php
")));
System.out.println(((Invocable)e).invokeFunction("hello", new Object[]{}));
System.out.println(((Invocable)e).invokeFunction("world", new Object[]{}));
((Closeable)e).close();
}
}
I have tested the above code using Java.inc from
http://php-java-bridge.sourceforge.net/pjb/java/Java.inc and JavaBridge.jar
from http://php-java-bridge.sourceforge.net/pjb/java/JavaBridge.jar.
Regards,
Jost Boekemeier
17. Aug 2009 4:44 nachm. schrieb am <
php...@li...>:
Hi Jost,
thank you for the reply.
> Is it a standalone web- or desktop application?
It is a standalone desktop application, but several tasks will also
interface a remote Web server, if/when a network connection is
available.
> Why do you need jetty for a desktop application?
I found it helps a lot to give structure and order to the
application(s). It takes care of potential CLASSPATH issues, it's easy
to setup and start/stop, and has a small footprint. Last but not least,
it keeps a door open for more advanced functionality I may need in the
future - this is just the first of (hopefully) several applications.
> If you want to use PHP as a plugin only, use the JSR 223 API.
I am currently trying to connect the Swing application with Jetty-hosted
PHP scripts via JSON-RPC, since the (RAW POST based) protocol is quite
efficient and easy to implement, both in PHP and in Java.
Sadly though, while I can readily access the RAW POST data via
php://input and can also write the JSON string received from Java to a
file, I see the fastcgi is unable to output the response ... looks like
the communication channel is corrupted somehow as soon as I fopen it for
reading.
On the grounds that I may be violating the standard way of interacting
with a servlet, I've been struggling the past few days to solve the
problem by using java_context() methods and properties, but without
success so far ... I'd appreciate some advice.
Massimo
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus
on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
php-java-bridge-users mailing list
php...@li...
https://lists.sourceforge.net/lists/listinfo/php-java-bridge-users
|