aspose file tools*
The moose likes Servlets and the fly likes please help..very urgent,applet-servlet communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "please help..very urgent,applet-servlet communication" Watch "please help..very urgent,applet-servlet communication" New topic
Author

please help..very urgent,applet-servlet communication

karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi all,
i am trying to pass a vector from a servlet to an applet,following is the code for both of them,the problem is that when i run the applet the vector which supposedly should receive the value from the servlet's vector,shows a value of zero,is the servlet isnt passing any vector to the applet afterall..
SERVLET PART:
public void senddata(HttpServletResponse response) throws IOException {
ObjectOutputStream outputToApplet = null;
try {
outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(vec);
outputToApplet.flush();
outputToApplet.close();
}
catch(Exception e) {
System.out.println("MyServlet: senddata() "+e.toString());
}
}
HERE "vec" is the vector which i am trying to pass to the applet,vec has been given some value in the while loop of the resultset.
APPLET PART:
public Vector getdatafromServlet() {
ObjectInputStream inputFromServlet = null;
try {
String servletLocation ="http://localhost:8080/servlet/myserv";
URL servletURL = new URL(servletLocation);
URLConnection servletConnection = servletURL.openConnection();
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches(false);
servletConnection.setRequestProperty("Content-Type","application/octet-stream");
inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
myvec = (Vector)inputFromServlet.readObject();
inputFromServlet.close();
}
catch(ClassNotFoundException e) {}
catch(Exception e){}
finally {
return myvec;
}
}
//this is the function in which i call getdatafromServlet()
void draw(Graphics g){
g.setColor(Color.black);
g.fillRect(0,v3,v2,1000);
g.setColor(Color.green);
g.drawRect(500,400,100,100);
myvec1 = getdatafromServlet();
System.out.println(myvec1.size());//shows a value of zero
Enumeration venum = myvec1.elements();
while(venum.hasMoreElements())
{System.out.println(myvec1);}
g.setColor(Color.blue);
g.setFont(new Font("TimesRoman",Font.BOLD,25));
g.drawString(msg,v4,v5);
}

please please help me..its very urgent
thanks a million
karan
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi all,
i checked my above code and found out that the connection is somehow not being established btw the applet and the servlet.I tried to print out a statement after the following line in the applet code but the line didnt print and this is how i came to know that the connection is not established.the line was
inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
what do i do now..please tell..
karan
Aruna Devi V.P
Greenhorn

Joined: Mar 07, 2001
Posts: 4
Hey,
I think two corrections you have to make.
1. Put some print messages in the exception handler code so that you will know exactly what goes wrong.
2. The request type in Applet side and servlet side should be set to java-internal/class.
3. Also if the Vector is going to hold any user defined classes they should serializable objects.
the servlet code should be like this:

in dopost method
first you have to set the response type
then you have to get the outputstream.

res.setContentType("java-internal/class" );
ObjectOutputStream out= new ObjectOutputStream (res.getOutputStream());
then fill the vector

and finally you have to write the vector object
out.writeObject(Rec);
out.flush();
out.close();

in the applet side
after setting
doInput(true)
doOutput(true)
doCache(false)
doDefaultCache(false)
set the request property to java-internal/class
then get Urlconnection object's inputstream and
ObjectInputStream os = new ObjectInputStream(con.getInputStream());
os.readObject(ss);
Hope this should help you.
All the best!
Regards,
Aruna
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi aruna,
i did the corrections that u suggested but its still not working.following are some facts:
1.)the vector that i am passing in the servlet to the applet doesnt have any user defined classes,it just holds simple double values.thats why i think it should not be serialized.correct me if i am wrong.
2.)u wrote in the reply that after the following lines i should fill the vector,
res.setContentType("java-internal/class" );
ObjectOutputStream out= new ObjectOutputStream (res.getOutputStream());
but the thing is that i have already filled the vector in my doGet method(ie when i executed the query and stored the result in a resultset and then added elements to the vector from the resultset).and then in the following statement
outputToApplet.writeObject(vec);
vec is the same vector.
So do u think that its necessary to fill the vector only in the doPost method or its OK the way i did.i think it should not be a problem.again correct me if i am wrong.actually i tried passing only a string form the servlet and i assigned a value to the string in the doPost method only but still the value assigned to the corresponding string variable in the applet was null..this is the reason of my belief that we can fill the vector beforehand itself.
3.)i put the follwing statement both in applet and servlet but it doesnt work:
res.setContentType("java-internal/class" );
please help me out and thanks a lot for your patience..
regards
karan
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi aruna,
i printed out stacktrace in the exception in the method for the applet and following is what the stacktrace says:

java.io.StreamCorruptedException: InputStream does not contain a serialized obje
ct
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:849
)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:168)
at Simple.getdatafromServlet(Simple.java:354)
at Simple.draw(Simple.java:379)
at Simple.paint(Simple.java:446)
at sun.awt.RepaintArea.paint(RepaintArea.java:298)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:193)
at java.awt.Component.dispatchEventImpl(Component.java:2665)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:319)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:10
3)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)
actually i am passing a VECTOR and a vector is indeed serializable...
please help me out with this..its very urgent...
regards
karan
Aruna Devi V.P
Greenhorn

Joined: Mar 07, 2001
Posts: 4
Hi Karan,
The usual way of passing objects from servlet to applet is have a clear distinction between the Servlet and the object to be passed.

what i mean by this is, donot have the manipulations and the data to be passed inside the servlet. Instead have a seperate public class in a seperate file. Implement Serializable for this class. Within this class, have the Vector as a memeber. In some method of the class, you do the database querying and fetching and fill up the vector.
Seggregate the database operations and data fetched, from the servlet..
Now in the servlet do not write anything in doget except directing the control to dopost..
public void doget(p1, p2)
{
dopost(p1, p2)
}

This is because you are expecting a post request for this servlet even if the request is made as get request you are redirecting the request to dopost as all the manipulations and request servicing operations are in dopost. do not spread the manipulations half in doget and half in dopost. This is because a post request directly comes to dopost method via service() method.
So in your case, the vector would not even have filled up.
Also you can ensure what is being sent by putting a simple debug message printing the object before writing the object in outputstream. You will know whether the object is empty or not.
The vector again must have Double objects and not double variables.

hope this helps you.
Regards,
Aruna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help..very urgent,applet-servlet communication