File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Applet to Servlet Communication (only two ways?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Applet to Servlet Communication (only two ways?)" Watch "Applet to Servlet Communication (only two ways?)" New topic
Author

Applet to Servlet Communication (only two ways?)

Ramiro Veramendi
Greenhorn

Joined: Oct 06, 2004
Posts: 3
All the examples that I've seen about Applet to servlet communication is in two ways, I mean it always has to be from the applet to the servlet and from the servlet to the applet.

My problem is that in my project I have to send some data to the Servlet and then the Servlet has to take the controll of everything, it isn't necesary to return to the applet, instead the Servlet has to make a mapping.findforward to another action....

If what I want to do is possible, please give some ideas about this....
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
read a book on servlets.


thanks & regards.


thanks & regards,<br />Sanjeev.
Harry Wood
Greenhorn

Joined: Jan 28, 2004
Posts: 16
What examples are you working from?
Are you sending serialised java objects in a HTTP request from applet to servlet?
This is what I do in my code:



The last three lines are to receive a response from the servlet. In my case this is also serialised java objects. Are you saying you don't want to receive a response? Maybe you could just drop these last 3 lines. Not sure.
Ramiro Veramendi
Greenhorn

Joined: Oct 06, 2004
Posts: 3
Thanks for your answers...

well this is what I have in my applet:

URL urlServlet = new URL("http://localhost:8080/triad/test.do");
URLConnection con = (URLConnection) urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type","application/x-java-serialized-object");
// send data to the servlet
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(template.getTemplate());
oos.flush();
oos.close();
// receive result from servlet
ObjectInputStream inputFromServlet = new ObjectInputStream(con.getInputStream());
Object result = (Object) inputFromServlet.readObject();
inputFromServlet.close();

And this is what I have in my Servlet:

response.setContentType("application/x-java-serialized-object");
//from the applet
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
Object template = (Object) inputFromApplet.readObject();
//to the Applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(respuesta);
oos.flush();
oos.close();

return mapping.findForward("successTemplate");


So this code works fine, I mean that I can send what ever I want from the applet to the servlet, and I can send also what ever from the Servlet to the applet...

But what I want is "....just to send something to the Servlet and then the servlet take the control", but in this scheme what is happening is when something is send to the Servlet the servlet gives the control again to the applet, and the conexion finish in the applet, and not in the Servlet.

I tried to erase the last 3 lines to recieve the response from the Servlet, and also i Tried to erase the code from the Applet, but when some "conexion code" from the applet and also from the Servlet is miss, the program gives the next exception:

If I erase the code to return something to the applet from the Servlet:
//to the Applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(respuesta);
oos.flush();
oos.close();

I got the follow error:

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)....................................

and if I erase the code to retrive something from the Servlet from the Applet:
// receive result from servlet
ObjectInputStream inputFromServlet = new ObjectInputStream(con.getInputStream());
Object result = (Object) inputFromServlet.readObject();
inputFromServlet.close();

"I can't retrieve anything from the Applet.........."

so, my question is that is it possible to have the applet to servlet communication just in one way (...just Applet --> Servlet)
Naveen Garg
Greenhorn

Joined: Oct 07, 2004
Posts: 2
No, you can not do that. In fact I had also come across to the same situation. What I found is that you have to redirect or forward from calling applet/servlet only.

Originally posted by Ramiro Veramendi:
so, my question is that is it possible to have the applet to servlet communication just in one way (...just Applet --> Servlet)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Applet to Servlet Communication (only two ways?)