You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by 최영규 <wa...@secure1.co.kr> on 2000/10/09 17:58:34 UTC
About Servlet-Applet Communication
I have difficulty in Servlet-Applet Communication.
I go around SUN Developer Connection and try as some pieces of
advice I saw there.
I could transfer parameter from Applet to Servlet.
Like it.
Using URL, URLConnection Object
URL url = new URL("http://hums/services/servlet/SomeServlet");
URLConnection uc = url.openConnection();
String qry = URLEncoder.encode( "qry" ) + "=" + URLEncoder.encode( qryString )
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type",application/x-www-form-urlencoded" );
DataOutputStream dos = new DataOutputStream( uc.getOutputStream() );
dos.writeBytes( qry );
dos.flush();
dos.close();
And in SomeServlet , HttpServletRequest req can catch parameter
with req.getParameter("qry"); using doPost Method or service method.
But I don't know how servlet give response applet back..
I tried HttpServletResponse.getOutputStream() or PrintWriter Object
or DataOuputStream(HttpServletResponse.getOutputStream());
But all failed in anyway..
ps. I want to know communication with Object transfer in addition to Primitive Type with DataOut(In)putStream..
Of course,I guess ObjectOut(In)putStream will be used with implementing Serializable Interface.
I want see any example..
Regard Choi..
Re: About Servlet-Applet Communication
Posted by Matt Goss <mg...@rtci.com>.
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
RE: About Servlet-Applet Communication
Posted by ??? <wa...@secure1.co.kr>.
Thank for ur kind response.
I had tried as you taught me but failed.-_-
<--applet side-->
ObjectOutputStream os = new ObjectOutputStream(uc.getOutputStream());
os.writeObject("leftItem");
os.flush();
os.close();
<--servlet side-->
try
{
temp = (String)is.readObject();
// Display the string
}catch (ClassNotFoundException e){
System.out.println("Class Not found Exception");
}
-----Original Message-----
From: Matt Goss [mailto:mgoss@rtci.com]
Sent: Tuesday, October 10, 2000 2:31 AM
To: tomcat-user@jakarta.apache.org
Subject: Re: About Servlet-Applet Communication
what you want to do is use the output stream from the response as an
object stream like this:
<on the servlet side>
Object myobject = new Object();
java.io.ObjectOutputStream os = new
ObjectOutputStream(resp.getOutputStream());
os.writeObject(myobject);
</on the servlet side>
<on the applet side>
ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());
Object myobject = (Object) ois.readObject();
ois.close();
</on the applet side>
That works for me...
Matt Goss
Re: About Servlet-Applet Communication
Posted by Matt Goss <mg...@rtci.com>.
what you want to do is use the output stream from the response as an
object stream like this:
<on the servlet side>
Object myobject = new Object();
java.io.ObjectOutputStream os = new
ObjectOutputStream(resp.getOutputStream());
os.writeObject(myobject);
</on the servlet side>
<on the applet side>
ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());
Object myobject = (Object) ois.readObject();
ois.close();
</on the applet side>
That works for me...
Matt Goss