tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Goss <mg...@rtci.com>
Subject Re: About Servlet-Applet Communication
Date Tue, 10 Oct 2000 14:05:55 GMT
I have also had a little trouble getting applets to write to the object
stream... if you are only using strings then an easy workaround is to
tack them on the end of the url as a name value pair and pull them out on
the server side like so:
<applet side>
    /**in example I am expecting a vector to be returned from the servlet
based on the results of the SQLSTRING*/
    //connect to servlet and pass the string
    URL url = null;
    URLConnection conn = null;
     try{
         url = new
URL("http://_mgoss:8100/test/servlet/dataserver?SQLSTRING="+URLEncoder.encode(SQLSTRING));

            conn = url.openConnection();
        }catch(Exception e){
            System.out.println("Error connecting to Servlet");
            System.out.println(e.toString());
        }
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

        //get dataobject back
        ObjectInputStream ois = null;
        Vector v = null;
        try{
            ois = new ObjectInputStream(conn.getInputStream());
            v = (Vector)ois.readObject();
            ois.close();
        }catch(Exception e){
            System.out.println("Error reading object input stream");
            System.out.println(e.toString());
        }
</applet-side>
<servlet-side>
/**the servlet waits to recieve a SQLSTRING and returns a vector with the
data through an object stream*/
    String SQLSTRING = req.getParameter("SQLSTRING");
        java.util.Vector v = doQuery(rdi, SQLSTRING);
        java.io.ObjectOutputStream os = new
ObjectOutputStream(resp.getOutputStream());
     try{
         os.writeObject(v);
     }catch(Exception E){
         System.out.println("Error on writing to Output Stream.");
         System.out.println(E.toString());
     }finally{os.close();}
</servlet-side>

Try this... :)
Matt


Mime
View raw message