I need to invoke a PHP method from java. Can I do that with request dispatcher?
-
I don't think, anyone can answer this question without any further information. Please describe your development environment! – guerda Mar 05 '09 at 14:11
-
possible duplicate of [Calling PHP function from Java](http://stackoverflow.com/questions/13748137/calling-php-function-from-java) – RivieraKid Dec 06 '12 at 20:19
-
1since this is the authorative question marked to many duplicates (such as [this](http://stackoverflow.com/questions/13748137/calling-php-function-from-java), [this](http://stackoverflow.com/questions/12897070/how-can-i-run-php-code-within-a-java-application) and [this](http://stackoverflow.com/questions/614995/calling-php-from-java)), the question should not be closed as not a real question. Voting to reopen. – eis Jan 18 '15 at 16:29
3 Answers
Here is a handy way to invoke a PHP method from within a Java application.
Runtime.getRuntime().exec("php <your script name>");

- 329
- 3
- 16

- 19,079
- 3
- 51
- 79
You'll need to make a separate request as though it was to an external site, but that will essentially be a full web request. There is no way to call a single PHP function from within Java unless you've got a PHP interpreter in your JVM, which seems highly unlikely.
You probably want to write the PHP side like you would write for an AJAX interface (using JSON or some loosely defined XML) or a SOAP interface.

- 8,079
- 2
- 29
- 32
The request dispatcher is only meant to forward to (or include) another web resources in your J2EE web app.
If you want to bring user to a PHP page from a J2EE webapp, issue a redirect.
If you want to process results of request to a php page in your J2EE webapp, try HttpURLConnection or the like.

- 5,283
- 22
- 24