I'm working on some stuff where I have an
applet that need to communicate with a database it's not allowed to acces because of the sandbox it run in. I use a servlet on the machine the applet was served from as kindof a proxy to do all the database work. I do about exactly what you're trying to do.
The applet (client) sends text commands (sql queries) to the servlet like this
try {
// Construct a URL referring to the servlet
java.net.URL url = new java.net.URL( "http://localhost:8084/myApp/myServlet");
java.net.HttpURLConnection connection = (java.net.HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("ContentType", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
// Send a POST message to the servlet
String sql = "sql=" + java.net.URLEncoder.encode(str, "UTF-8");
sql = sql + "&command=select";
byte bytes[] = sql.getBytes();
//at this point "sql" is the text I want to send
connection.setRequestProperty("Content-length", "" + bytes.length);
connection.connect();
OutputStream out = connection.getOutputStream();
out.write(bytes);
out.flush();
//in my case, the servlet return a binary object (a Vector)
//you may want to just return text or nothing
// Get the response as an ObjectInputStream
InputStream in = connection.getInputStream();
ObjectInputStream result = new ObjectInputStream(in);
// Read the Date object from the stream
Object obj = result.readObject();
v = new myRowSet((java.util.Vector)obj);
// Return the Vector containing the data
return v;
}
catch (Exception e) {
//javax.swing.JOptionPane.showMessageDialog(m_App, "An Error Occured Connecting To The Database!", "SQL Error", javax.swing.JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
return null;
}