aspose file tools*
The moose likes Servlets and the fly likes Calling simple PHP from Servlet or JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling simple PHP from Servlet or JSP" Watch "Calling simple PHP from Servlet or JSP" New topic
Author

Calling simple PHP from Servlet or JSP

Renato Losio
Ranch Hand

Joined: Nov 23, 2005
Posts: 99
Hello.

I have a jsp + servlet web app. I have to problem when I need to bridge to external technologies:

From the user registration page I would like to call a PHP script to send a message to the user when the process is completed. Note that the script is already available and I need to pass the phone number as a string. Is there a quick solution from the JSP page? I don't need to control the flow or catch any error, just call the sendMessage(String phoneNumber) script.

I guess a bit more complex, I don't know if a php-bridge is required. Same as above but I need to catch a return value from the PHP script. The validateCode(String phoneNumber) is pretty simple. It may return a integer code (from 1 to 5) and I would like from a servlet - or eventually a JSP - to handle the code and do some logic.

Any idea where to start? Any useful link?

Thanks.

Renato


Renato Losio - www.arsenio.it - renatoweb@arsenio.it
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

There's really no such thing as a Java/PHP bridge, but these technologies are malluable enough that connecting them is pretty simple.

For example, one way to connect them in the context of a web environment is to have them make http calls to each other. You can restrict external people from doing this by having the php/jsp check to make sure the person executing the page has a local ip address. The advantages are that the programs never look at the internal structure of each other and therefore you can swap in JSP-to-JSP communication or PHP-to-PHP communication if you get rid of one of the technologies.

The other option is to have them drop to command line. For example, calling Runtime.exec() in java to run a php script. This is usually very fast but it might require a tighter coupling than the previous method in that your php calls may be extremely machine sensitive.


My Blog: Down Home Country Coding with Scott Selikoff
Renato Losio
Ranch Hand

Joined: Nov 23, 2005
Posts: 99
Hi Scott,

many thanks for your prompt and detailed reply.

I will give both the options a try and see which one suits better my web app. Looks like I can call (quite) easily the PHP script but I won't be able to collect what I have back from the call. Is my assumption correct?

Cheers,

Renato
Bram Maes
Ranch Hand

Joined: Sep 27, 2006
Posts: 32
I'm not really an expert in this, but isn't it possible to execute a JavaScript that redirects to the PHP page, and use a GET to pass the phone number ?

And maybe you could use the same to get output from the php page ?(and redirect vs jsp)
[ October 20, 2006: Message edited by: Bram Maes ]

SCJP 5.0, SCEA part I
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Originally posted by Bram Maes:
I'm not really an expert in this, but isn't it possible to execute a JavaScript that redirects to the PHP page, and use a GET to pass the phone number ?



Eh, its never a good idea to base your primary logic (and communication) in javascript. Its not robust enough. Besides, I don't see why the javascript is necessary when PHP and Servlets can connect to each other directly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling simple PHP from Servlet or JSP