Sende dataset mellom applet og servlet
Hei, holder på med en applet som skal kommunisere med en Http Servlet. I fra appleten skal man kunne opprette, slette og kopiere dataer fra en eller flere tabeller. Bruker streaming imellom appleten og servleten, og servleten ligger på samme maskin som sql serveren hvor databasen er. Det jeg gjør er: får kontakt med servleten, kjører et sql kall fra servleten mottar 4 rader, og prøver å sende disse tilbake til appleten, men på applet siden så er det ingen rader i dataset'et. Er ganske fersk i java så det er sikkert noen grunnleggendes feil her... Her er koden:Appletsiden:
private DataSet getDetailUsingHttpObject() {
// Retrieve the current time using an HTTP object-based connection
try {
// Construct a URL referring to the servlet
URL url = new URL(getCodeBase(), "http://localhost:8080/XXXXX/ZZZZZ");
// Create a servlet.HttpMessage to communicate with that URL
HttpMessage msg = new HttpMessage(url);
// Construct a Properties list to say format=object
Properties props = new Properties();
props.put("format", "object");
// Send a GET message to the servlet, passing "props" as a query string
// Get the response as an ObjectInputStream
InputStream in = msg.sendGetMessage(props);
ObjectInputStream result = new ObjectInputStream(in);
// Read the Date object from the stream
Object obj = result.readObject();
detail = (com.borland.dx.sql.dataset.QueryDataSet)obj;
int test3 = detail.columnCount();
return detail;
}
**********************************************************
På serverside:
if ("object".equals(request.getParameter("format"))) {
DataSet ars = SqlTools.getDetailSection(dm.getDatabase());
int test2 = ars.columnCount();
Array test = null;
test2 = ars.columnCount();
ObjectOutputStream out2 = new ObjectOutputStream(response.getOutputStream());
out2.writeObject(ars);
}
***********************************************************
HTTPMESSAGE programmet:
public class HttpMessage {
URL servlet = null;
String args = null;
public HttpMessage(URL servlet) {
this.servlet = servlet;
}
// Performs a GET request to the previously given servlet
// with no query string.
public InputStream sendGetMessage() throws IOException {
return sendGetMessage(null);
}
// Performs a GET request to the previously given servlet.
// Builds a query string from the supplied Properties list.
public InputStream sendGetMessage(Properties args) throws IOException {
String argString = ""; // default
if (args != null) {
argString = "?" + toEncodedString(args);
}
URL url = new URL(servlet.toExternalForm() + argString);
// Turn off caching
URLConnection con = url.openConnection();
con.setUseCaches(false);
return con.getInputStream();
}
// Performs a POST request to the previously given servlet
// with no query string.
public InputStream sendPostMessage() throws IOException {
return sendPostMessage(null);
}
// Performs a POST request to the previously given servlet.
// Builds post data from the supplied Properties list.
public InputStream sendPostMessage(Properties args) throws IOException {
String argString = ""; // default
if (args != null) {
argString = toEncodedString(args); // notice no "?"
}
URLConnection con = servlet.openConnection();
// Prepare for both input and output
con.setDoInput(true);
con.setDoOutput(true);
// Turn off caching
con.setUseCaches(false);
// Work around a Netscape bug
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// Write the arguments as post data
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(argString);
out.flush();
out.close();
return con.getInputStream();
}
// Converts a Properties list to a URL-encoded query string
private String toEncodedString(Properties args) {
StringBuffer buf = new StringBuffer();
Enumeration names = args.propertyNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = args.getProperty(name);
buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value));
if (names.hasMoreElements()) buf.append("&");
}
return buf.toString();
}
}