This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
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....
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.
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();
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)