Hi, I'm looking to update a MySql database from an applet.
The 2 roots I was thinking were:
Applet->php->database
Applet->servlet->database

But my webhost doesn't allow servlets/jsp, so I would like to try the php route.
I've looked on the internet, but I can't find anything concrete to use, just lots of random waffle.

Essentially I just want to pass a String from my applet to a .php page on the server (Or a function from a php page), possibly by retrieving the String like a Form parameter on an html page.

Can anyone help?
Thanks

Recommended Answers

All 10 Replies

Why not just use Applet->database? I think you may have to sign the applet, but you can use JDBC within an applet.

I don't want to use signed applets, they are annoying.
I know I can use php, but doesn't seem theres any code on the internet to show this except for scraps of random bits of code which doesn't help.

You would just need to make an HTTP request to the server with the appropriate GET or POST commands.

Here's an example for making a post request:
http://www.exampledepot.com/egs/java.net/Post.html

A GET request would be even easier since the data is actually in the url string.

Thanks very much!

I'll have a look at the code shortly.
Its nice to get some good comments instead of vague comments that dont help.

Thanks

Ok, I got it to work!

Ok, it works, but this is bonkers, if I miss out the line...
conn.getContentType();

Then it wont work, but it always works with it.

I've added the class below, if anyone wants to try it, they will see what I mean if they comment out

conn.getContentType();

Then run program a few times, and there is no change to textfile on server.
But when the line is included it should run everytime.

Its so weird, and I can't explain it, please can some other people try to see whats going on.

Thanks.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JApplet;
 
public class Main extends JApplet
{
 private static final long serialVersionUID = 1L;
 public Main()
 {
  System.out.println("IN CONSTRUCTOR");
  
  writeToFile();
  String data=readFile("[URL]http://www.colin-java.co.uk/Test/TESTFILE.txt[/URL]");
  
  System.out.println("data="+data);
 }
 
 
 public static String readFile(String location)
 {
  URL dataStream=null;
  try
  {
   dataStream = new URL(location);
  }
  catch (MalformedURLException e2)
  {
   e2.printStackTrace();
  }
  BufferedReader in=null;
  
  try
  {
   in = new BufferedReader(new InputStreamReader(dataStream.openStream()));
  }
  catch (IOException e2)
  {
   e2.printStackTrace();
  }
  String fullString="";
  String str;
  try
  { 
   int counter=1;
   while((str = in.readLine()) != null)
   {
       fullString+=(counter++)+". "+str+"\n";
   }
  
  }
  catch (IOException e1)
  {
   e1.printStackTrace();
  }
  
  return fullString;
 }
 
 public void writeToFile()
 {
  try
  { 
   URL url = new URL("[URL]http://colin-java.co.uk/Test/data.php[/URL]");
   URLConnection conn = url.openConnection();
   conn.getContentType(); 
  }
  catch (Exception e)
  {
   System.out.println("WRITE TO FILE ERROR: "+e.getMessage());
  }
 }
}

I would imagine the php script is not being executed by simply opening the connection. openConnection() by itself may not generate a request on the script. The getContentType() is making a data request from the connection and that is what probably triggers the execution. getContent() would probably do the same.

Thanks, I asked in javaranch too, they said the same thing.
Seems a bit unusual to do this though, I was trying everything to get it to work, then I printed out some data from 'conn' and it worked.
Gonna take a look at the API, that should help.

Thanks

The API is a little bit vague on that, but since normally one makes a request for content or executes some read or write operation against the connection, the details of how scripts get executed probably isn't germaine to the general description of the API.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.