wood burning stoves 2.0*
The moose likes Applets and the fly likes Java applet communicating with internet based mySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Java applet communicating with internet based mySQL" Watch "Java applet communicating with internet based mySQL" New topic
Author

Java applet communicating with internet based mySQL

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I'm not sure this is the correct forum, but here goes. I may have asked this question before, if so I apologise.

Ok my java chess applet is hosted on a web server on the net somewhere. I have created some user interactivity with mySQL by having an HTML form send data to a php script which queries the mySQL server then dynamically creates html code including param tags which feeds the queried data to my chess applet via the init() method. So far so good.

I'd like to have data that originates with my (client side) applet feed back to mySQL on the server. It has been suggested to me that I should use Ajax to call a php script. My initial research says this doesn't sound right. i.e. Ajax is browser scripting which uses something called XMLHttpRequest to speak asynchronously to the Web Server. That in itself is ok, except intuitively it doesn't seem right that my java applet should have to talk to a browser script in order to gt the job done.

So, I'd appreciate some insight from someone who has been down this road, or knows the technology. I just need to know where I should focus my attention.

I should point out that I am using a free webhosting service that has limited functionality, and that means no server side java.

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

I don't see why your applet code shouldn't send the HTTP requests directly to the server and handle the response itself.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Paul Clapham wrote:I don't see why your applet code shouldn't send the HTTP requests directly to the server and handle the response itself.


That confirms what I was thinking. The java.net package suggests itself, in particular there is the URLConnection and HttpURLConnection classes, so I guess I start there. I suppose that, as in HTML forms, there is some kind of POST function in one of these classes that works in a similar fashion to the POSTing of HTML form data to php. As far as handling the response goes, that shouldn't need to be anything more than a confirmation that the called php script was succesful in inserting the data that was posted to it by java.

Anyways, I'm just thinking out loud here, we'll see how it goes.

regards.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

Yes, that's right. You can write your own HTTP-POST code using those classes or you could use the Apache HttpClient open-source package. Using Apache means you don't have to deal with all the grubby details of HTTP (especially the POST method) but it also means that its jar file increases the size of your applet download.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

Yup. Just to be clear on one thing: you couldn't talk directly to MySQL from an unsigned applet. That violates sandbox rules. Plus, opening a MySQL port directly onto the open Internet it just begging for trouble.

Proxying the request through a servlet or PHP application is a process known as "HTTP Tunnelling". It's a time-honored tradition that I was using all the way back in 2000, long before the word "AJAX" meant anything other than a Greek warrior or a cleaning product.

Java's URL Request classes make this process quite simple - even the cookies are automatically handled for you. You can pretty much do and HTTP GET or POST request that you would have done via a browser. Which means that if you stick to text, it's easy to test outside the applet.


Customer surveys are for companies who didn't pay proper attention to begin with.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Thanks Tim, duly noted. I don't think I knew about HTTP Tunneling, but it will give me a new angle from which to search for information
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java applet communicating with internet based mySQL
 
Similar Threads
Java code sitting along with php.. assisting
browser-based game question
Randomly displaying images from a directory with JavaScript?
Writing to a URLConnection object
using Java with an internet based mySQL Server?