JApplet and PHP ..

Hi all,I just want to know how can I tell my applet to execute a PHP code when a button is clicked...thanks in advance.
[140 byte] By [Raieda] at [2007-11-27 9:59:52]
# 1

It depends on what you mean by "execute a PHP code".

An applet can easily open an HTTP connection to the site that it came from, and slightly less easily open up a connection to any valid URL. The URL can be to a page created with PHP.

If you want to take the PHP code and run it locally, within the applet itself, then that's a whole other thing.

paulcwa at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for your response ...

I am trying to read and write files from my applet that reside in the same directory. But I discovered that it is so hard to do so and its much simpler to do the writing and reading using a php code. So, I want to write a php code that reads the data and then sends it to my applet (which i can do using <param name=" " value="<?php ........... ?>">) then after the applet is done with processing the data, I want the applet to send the precessed data back to the php page to write it.

That is: how can I ask a java applet to send some data to the php page that the applet reside in so the page writes the data to a file.

regards,

Raieda at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...
# 3
Or if executing a php code locally within the applet is easy. I will apreciate telling me how can I do it.thanks in advance.
Raieda at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...
# 4

> That is: how can I ask a java applet to send some

> data to the php page that the applet reside in so the

> page writes the data to a file.

You can send data from the applet running on the user's computer to a URL serviced by PHP running on the web server simply by opening HTTP connections to it.

Read the Java networking tutorial, and look at the API docs for java.net.URL and java.net.HttpURLConnection.

paulcwa at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...
# 5

> Or if executing a php code locally within the applet

> is easy. I will apreciate telling me how can I do

> it.

This is the exact opposite of what you said in the previous post, do you know that?

I strongly doubt it's easy. You'd need a PHP interpreter to be executed within the JVM. Maybe someone has already written that (perhaps using the scripting language support in Java 6?), but if so I haven't heard of it.

But it's not clear what the point of doing this would be anyway.

paulcwa at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...
# 6

thanks for your response...

the main point of doing this is that writing and reading txt files on the server using java is TOO hard. And its much easier to do it using PHP.

the applet is running in a page named "index.php" which resides in the public html folder. So I only want to send data from the applet to this index.php page. So, I am not sure if I really need to open connections.

In other words, I know how to send data from the index.php page to the applet that is running on it. this can be accomplished by using this line of code:

<param name="" value="<?php ?>" />

and then calling this parameter from the applet.

my question is: how can I do it the other way around ? that is, how can I tell the applet to send a variable to the index.php page.

OR is there is an easy way to execute the php code directly from the applet.

regards.

Raieda at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...
# 7

You have to open connections.

You seem to have a fundamental misunderstanding of how an applet works. An applet is a java program that runs on the user's computer. This is different from a web page produced by PHP running on the server. The applet may be displayed to the user as embedded in the PHP page, but it's actually being run on a completely different machine.

If you want your applet to write files on the server (or, more accurately, to cause files to be written on the server) then you're going to have to open some kind of network connection between the applet and the server. The easiest way is probably to use HTTP to a PHP page (either the same one or a different PHP page with different responsibilities).

paulcwa at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...
# 8
thanks alot ..I just understood what you are saying :)now, where do you advise me to read for how to make http connections :) ?Thanks again
Raieda at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...
# 9
http://java.sun.com/docs/books/tutorial/networking/index.html
paulcwa at 2007-7-13 0:30:57 > top of Java-index,Java Essentials,Java Programming...