开发者

Save Java Applet Variable to Mysql

Hi I've done a lot of research on the best way to communicate between a java applet and MySql Database. I have a tune player which I have students logging onto, it's a java applet with a speed slider. I want to save the speed that they play e开发者_如何学运维ach tune at so it goes back to the same speed the next time they open that tune. It seems I have a number of options, none of which seem very neat.

  1. Use a javascript function to periodically check the speed and save it to a cookie, then each page of the site would have to check cookies relationg to each tune.
  2. Make each link on the page call a javascript function to check the speed variable in the applet and add it to a perameter in the url then redirect so the next php page can save the speed to a database. This way when the user navigates away the speed will be saved, but this won;t work if the back button is used.

Is there a better way of doing this?


Use the JNLP API and the problems should be solved.

Since Java 1.6.0_10+, it is possible to use the Java Web Start API services (JNLP API) within an embedded applet. The JNLP API provides the PersistenceService. Here is a small demo. of the PersistenceService.

If the user hits the back button (or otherwise leaves the page), the destroy() method will be called. Override the destroy method and persist the data at that time.


No need to use JavaScript.

The java code below posts variables to a PHP script on the web server then shows the server response on the console

private void post()
  throws MalformedURLException, IOException
{ URL url;
  URLConnection con;
  OutputStream oStream;
  String parametersAsString;
  byte[] parameterAsBytes;
  String aLine; // only if reading response

  parametersAsString = "msg=hi&to=memo";
  parameterAsBytes = parametersAsString.getBytes();
  // send parameters to server
  url = this.getCodeBase();
  url = new URL(url + "scriptfile.php");
  con = url.openConnection();
  con.setDoOutput(true);
// setDoInput(true); // only if reading response
  con.setDoInput(false);
  con.setRequestProperty("Content=length", String.valueOf(parameterAsBytes.length));
  oStream = con.getOutputStream();
  oStream.write(parameterAsBytes);
  oStream.flush();

 // read response from server & show the server response on the Java console or whatever ...
  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  aLine = in.readLine();
  while (aLine != null)
  { System.out.println(line);
    aLine = in.readLine();
  }
  in.close(); 


  oStream.close();
} 


I'd suggest you get the applet to update the database. Whenever the speed slider changes you can fire off an update to the database, or you might need to coalesce multiple changes into one request depending on usage.

When the applet changes tune it can also do its own lookup of the correct speed.

Note that the applet will probably not be able to hit the database directly - browsers should restrict what I/O operations are available to applets - but you should be able to get the applet to hit a URL on the server that will actually perform the update. Signing the applet may let you hit the database but you should read up on the applet security model and the various browser quirks first.


It's not really clear how all of this is set up since you don't have a lot of details. However, assuming that you have an actual Java applet, I'd say the following:

  1. If the Java applet requires a login (that is, it knows who the user is) then it can store the preference on the server. To do this you could have the applet connect to the server using JDBC, which isn't generally a good idea for internet-facing applets, or you could have the applet send a message to a server process such as a web server. That process connects to the mysql server.
  2. The applet can communicate directly with the browser using Javascript. So you can have the applet set the cookie when the slider changes, instead of having the Javascript set it.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜