Use FileOutputStreams to send arrays of serializable Objects to
HTTPServlet.service(). Here is a pared down version of how I do it:
public class YourServlet extends HttpServlet{
public void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try {
ObjectInputStream inputFromApplet = new
ObjectInputStream(request.getInputStream());
Object connArray[] = (Object[])inputFromApplet.readObject();
inputFromApplet.close();
//now, in my servlet, if I am, for example, looking for a string
in element 3 of the array of objects passed:
(String)connArray[3]
//just cast to whatever it is when sent
sometimes, I use
element 0 as an ArrayList of the types that follow!
((ArrayList)connArray[0])
(Double)connArray
// see?
//now, you can even send things back to the applet this way
// send objects back to applet
ObjectOutputStream outputToApplet = new
ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(/*some serializable object such as
Object[] as we sent to the servlet*/);
outputToApplet.flush();
outputToApplet.close();
}catch(Exception e){}
}
}
//and in your applet, call the servlet and pass it objects:
try {
String host = applet.getDocumentBase().getHost();
URL servletURL = null;
String servletURLstring;// eg "/servlets/YourServlet"
servletURL = new URL("http://" + host + servletURLstring);
System.out.println("servlet invoked:" + servletURL);
URLConnection uc = servletURL.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type","application/octet-stream");
ObjectOutputStream objOut = new
ObjectOutputStream(uc.getOutputStream());
int sz=5; //telling it the size in advance
Object conArray[] = new Object[sz];
conArray[0] = //whatever objects I
conArray[1] = //wish to send...
objOut.writeObject(conArray);
objOut.flush();
objOut.close();
// get objects from servlet
ObjectInputStream objIn = new ObjectInputStream (uc.getInputStream());
String returnString = (String)objIn.readObject();
objIn.close();
catch (Exception e) {
e.printStackTrace();
}
//Good luck, pls advise that you got this to work. -Ike