How is an applet to file data into a database on the web server?
This question is similar to another a few weeks ago:
How does a Java applet extract data
from a database on the server?
A web server can generate parameters to the applet.
Fine, but how will the applet return data?
How does an applet store data
in a database on the web server?"
Again, SQL would be the easiest solution, but there are a number of snags:
- Security
- The applet needs a user id and password in order to connect to a database.
Preferably user id and password should remain safely on the server.
- 3 independent tiers
- Most Java applets are thick clients, a combination of
GUI- and
business object layer.
SQL code belongs in the data layer.
The object layer must interface with this data layer,
even if it is located remotely on the web server.
Dear Mrs S.Q.L. Database,
The postman has a letter for you:
.
Would you please store the contents?
And do drop me a line.
|
Ideally the applet would be completely independent from the database, just posting a message.
The programs on the server can safely connect to the database and do its bit of SQL.
(see PHP example).
Solution 1: a URL with parameters
private void showDocument()
throws MalformedURLException
{ URL url;
url = this.getCodeBase();
url = new URL(url + "index.php?msg=hello&to=world");
// ask browser to exit applet and show page
this.getAppletContext().showDocument(url);
}
|
An applet can call a URL
(developer.irt.org/script/4023.htm)
asif the user has clicked on an HTML link.
A URL may contain parameters for the software on the server side.
Nice, but:
- The length of a URL is limited, whatever it is
(faqts.com/knowledge_base/...).
This limitation may cause problems when sending a few hundred parameters.
- Another disadvantage is that parameters in a URL are visible.
The user can bookmark the URL, including the parameters.
When the user redisplays the bookmarked page, unintentional reprocessing will occur at the same time.
Solution 2: POST
Does an applet have the ability to POST variables?
Yes, it does.
And gone are the security problems and visibility of parameters, plus the 3 layers are neatly seperated!
Stunning, stunning, stunning.
A piece of Java code at
forums.java.sun.com/...
offers hope with a HttpURLConnection.
Alas, the applet crashes with a ClassCastException in the statement:
HttpURLConnection http = (HttpURLConnection)url.openConnection();
Grmbl, the JVM's of the different browsers have annoying differences.
The browser specific classes for HttpURLConnection differ too much.
It takes a bit of puzzling and trial and error to get things working.
The java code below posts variables to a PHP script on the web server
private Vector postResult()
throws MalformedURLException, IOException
{ URL url;
URLConnection con;
OutputStream oStream;
String parametersAsString;
byte[] parameterAsBytes;
String aLine; // only if reading response
parametersAsString = "msg=hello&to=world";
parameterAsBytes = parametersAsString.getBytes();
// send parameters to server
url = this.getCodeBase();
url = new URL(url + "index.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();
oStream.close();
}
|
Dear Mr. J. Applet,
Received your message.
Stored contents well.
|
The server generates a response, lines with text.
The applet can read this generated response and use it as input.
The example below shows the server response on the Java console.
This piece of Java code will be between the oStream.flush() and the oStream.close().
oStream.flush();
// read response from server
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();
|
It works.
Good, I'm happy.
Got a Java applet talking with a server application:
- no messing about with secret passwords,
- and with a neat separation between the object and the database layer.
Till next week,
Nut
|