Link to home
Start Free TrialLog in
Avatar of icefire
icefire

asked on

ObjectOutputStream.writeObject() & ObjectInputStream.readObject()

I am trying to using an applet communicate with a servlet.  I established the communication.  And want to do:

1. applet send "Register" message to the servlet.
2. servlet send "Register Accept" message to the applet.
3. applet send "Hello" to the servlet.
4. servlet say "Hello!  How are you!" back.

step 1 and 2 are success.  But step 3 failed.

Could any one please tell me what is wrong?

=======================================

This is the Servlet.



import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.text.*;
import java.math.*;
import java.lang.Runtime;
import javax.servlet.*;
import javax.servlet.http.*;

public class BLServlet extends HttpServlet {

      // for integration of BLServlet
      // declare variables
      Socket agentsocket = null;
        ObjectInputStream   inputFromApplet = null;
         ObjectOutputStream  outputToApplet = null;

          JFrame frame;
          JTextArea textArea;
      String command = new String();
      String outMsg = new String();

        static boolean realsim;
        static int iStep = 0;
        static int iterStep = 1;


         public void sendToApplet(String info) {

                 try {
                  outputToApplet.writeObject(info);
                  outputToApplet.flush();
            } catch (IOException ie) {
                  textArea.append("Error sending Information to Applet: "+ie);
                  System.exit(-1);
            } // end of try/catch

            try {
                  Thread.sleep(3000);
            } catch (InterruptedException exc) {
                  return;
            }
      } // end of sendInfoToApplet method


      public String receiveFromApplet() {

              String  inMsg = "";
              int     gotIt = 0;

              textArea.append("read from applet\n");

              while (gotIt <= 0 ) {
                           try {
                                 // write info to Applet
                            inMsg = (String) inputFromApplet.readObject();
                            gotIt = 1;
                      } catch (Exception e) {
                        textArea.append("Error reading Information from Applet:\n  "+e+"\n");
                            gotIt--;
                            if (gotIt < -5 ) gotIt = 1;
                        try {
                                Thread.sleep(3000);
                          } catch (InterruptedException exc) {
                              return "";
                          }
                  }
            } // end of try/catch

              textArea.append("read Msg: "+inMsg+"\n");
              return inMsg;
      } // end of sendInfoToApplet method


          public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

             try {

                        frame = new JFrame("test");
                        frame.setTitle("BlowLoop Servlet");
                        frame.setVisible(true);
                        frame.setSize(800,600);

                        textArea = new JTextArea();
                        textArea.setSize(500, 400);
                        textArea.setBackground(new Color(255, 255, 255));
                        textArea.setLocation(0, 30);

                        JScrollPane sp = new JScrollPane(textArea);
                        sp.setSize(700,500);
                        sp.setLocation(50, 20);

                        Container c = frame.getContentPane();
                        c.setSize(800, 600);
                        c.setLayout(null);
                        c.add(sp);

                        frame.show();
                        textArea.append("start of main\n");

                  inputFromApplet = new ObjectInputStream(request.getInputStream());
                  String inMsg = new String(receiveFromApplet());
      
                  // read the contents of the message
                  if (inMsg.equals("Register")) {
                            outMsg = "Register Accept";
                        }
                        else {
                            outMsg = "MISTAKE2";
                        }

                        textArea.append("output message: "+outMsg+"\n");

                  outputToApplet = new ObjectOutputStream(response.getOutputStream());
                        textArea.append("object output stream: "+outputToApplet.toString()+"\n");
                  sendToApplet(outMsg);

                  inMsg = receiveFromApplet();
                  textArea.append(" - BLServlet - from Applet: "+inMsg+"\n");
                  inputFromApplet.close();

                  if (inMsg.equals("Hello")) {
                            outMsg = "Hello!  How are you!";
                        } else {
                            outMsg = "MISTAKE3";
                        }

                    textArea.append("output message: "+outMsg+"\n");
                  sendToApplet(outMsg);

              } catch (Exception exc) {
                    textArea.append("Exception: "+exc.toString()+"\n");
                  return;
            } // end of try/catch
      } // end of doPost method
}

---------------------------

This is part of the applet:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;


class AppletThread implements Runnable {

    BlowLoopApplet  _owner;
    ObjectOutputStream  outputToServlet;
    ObjectInputStream  inputFromServlet;


    AppletThread(BlowLoopApplet owner) {
        _owner = owner;
        _owner.textArea.append("Applet thread initial....\n");
    }


    public boolean establishConnection() {

        try {

            _owner.textArea.append("run start...please wait......\n");
            String location = "http://111.111.11.111:8080/servlet/BLServlet";

            URL servletURL = new URL(location);

            URLConnection servletConnection = servletURL.openConnection();
              _owner.textArea.append("URL = "+location+"\n");

            servletConnection.setDoOutput(true);
            servletConnection.setDoInput(true);
            servletConnection.setUseCaches(false);
            servletConnection.setRequestProperty("Content-Type", "application/octet-stream");

            _owner.textArea.append("servlet connection: \n  "+servletConnection.toString()+"\n");

            outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());

            _owner.textArea.append("output to servelt:  "+outputToServlet.toString()+"\n");

            //outputToServlet.writeObject("Register");
            //outputToServlet.flush();

            write("Register");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException exc) {
                _owner.textArea.append("Cannot Sleep.\n");
                return false;
            }

            inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
            _owner.textArea.append("input from servlet:   "+inputFromServlet.toString()+"\n");

            String st = read();
            _owner.textArea.append(st+"\n");

        } catch (Exception e){
            _owner.textArea.append("Error in establish connection: \n");
            _owner.textArea.append(e.toString()+"\n");
            _owner.stop();
            _owner.destroy();
            return false;
        }

        return true;
    }


    public void write(String st) {

        try {
            _owner.textArea.append("Send msg: "+st+"\n");
            outputToServlet.writeObject(st);
            _owner.textArea.append("Sent. \n");
            outputToServlet.flush();
            _owner.textArea.append("Flush. \n");
        } catch (IOException ioe) {
            _owner.textArea.append(ioe.toString()+"\n");
        }
    }


    public String read() {

        try {
            Object inMessage = new Object();
            inMessage = (Object) inputFromServlet.readObject();
            String strMess = inMessage.toString();
            return strMess;
        } catch (Exception ioe) {
            _owner.textArea.append(ioe.toString()+"\n");
            return "MISTAKE";
        }
    }


    public void run() {

        String  inMsg = "";

        establishConnection();

        _owner.textArea.append("output-to-servelt:  "+outputToServlet.toString()+"\n");
        _owner.textArea.append("input-from-servlet: "+inputFromServlet.toString()+"\n");

            write("Hello");
            _owner.textArea.append("sent: Hello\n");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException exc) {
                _owner.textArea.append("Cannot Sleep.\n");
                return;
            }

            inMsg = read();
            _owner.textArea.append("Receive: "+inMsg+"\n");

        _owner.textArea.append("End of run\n");
    }
}
Avatar of Venci75
Venci75

1. NEVER use System.exit(-1); in a servlet! you can just thow an exception
2. Instead of using ObjectInputStream and ObjectOutputStream classes you can use just:
InputStream. To write to the stream use:
3. It is not a good idea to send/receive many messages with one conenction. At least you can't do it with the supported by Java HTTP 1.0. You need HTTP 1.1 support.
       
Applet:
1. You don't need the line
Object inMessage = new Object();
in the read() method. It could be:
return (String)inputFromServlet.readObject();

To solve this problem use separate connections for the "Register" and the "Hello" commands.
Avatar of icefire

ASKER

How to use HTTP 1.1?
Avatar of icefire

ASKER

I changed the doPost function, add a new class ServletThread.

ObjectInputStream  inputFromApplet = new ObjectInputStream(inputStream);

throw exception

 java.io.IOException: Stream closed

Why?

==============================

public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        try {

            textArea.append("start of doPost\n");
            ServletThread servletThread = new ServletThread(this, request, response);
            new Thread(servletThread).start();
        } catch (Exception exc) {
            textArea.append("Exception: "+exc.toString()+"\n");
            return;
        } // end of try/catch
} // end of doPost method

    private class ServletThread implements Runnable{

        BLServlet  blServlet;
        HttpServletRequest request;
        HttpServletResponse response;

        public ServletThread(BLServlet bls, HttpServletRequest request1, HttpServletResponse response1){
            blServlet = bls;
            request = request1;
            response = response1;
            textArea.append(blServlet.toString()+"\n");
            textArea.append(request.toString()+"\n");
            textArea.append(response.toString()+"\n");
        }

        public void run(){

        try {

            textArea.append("servlet thread run() start.\n");
            InputStream  inputStream = request.getInputStream();
            textArea.append("get input stream: "+inputStream.toString()+"\n");
            ObjectInputStream  inputFromApplet = new ObjectInputStream(inputStream);
//               ObjectInputStream  inputFromApplet = new ObjectInputStream(request.getInputStream());
               String  inMsg = "";
            int     gotIt = 0;

            textArea.append("read from applet\n");

            while (gotIt <= 0 ) {
                  try {
                       // write info to Applet
                    inMsg = (String) inputFromApplet.readObject();
                    gotIt = 1;
                  } catch (Exception e) {
                       textArea.append("Error reading Information from Applet:\n  "+e+"\n");
                    gotIt--;
                    if (gotIt < -5 ) gotIt = 1;
                     try {
                           Thread.sleep(3000);
                      } catch (InterruptedException exc) {
                          return;
                      }
                }
              } // end of try/catch

            textArea.append("read Msg: "+inMsg+"\n");

               inputFromApplet.close();

               // read the contents of the message
               if (inMsg.equals("Register")) {
                outMsg = "Register Accept";
            }
            else if (inMsg.equals("Hello")) {
                outMsg = "Hello! How are you!";
            }
            else {
                outMsg = "MISTAKE2";
            }

            textArea.append("output message: "+outMsg+"\n");

              ObjectOutputStream  outputToApplet = new ObjectOutputStream(response.getOutputStream());
            textArea.append("object output stream: "+outputToApplet.toString()+"\n");

              try {

                   outputToApplet.writeObject(outMsg);
                   // flush the ObjectOutputStream
                   outputToApplet.flush();
              } catch (IOException ie) {
                   textArea.append("Error sending Information to Applet: "+ie);
              } // end of try/catch

            outputToApplet.close();
            } catch (Exception e) {
                textArea.append("Error in servlet thread run():\n  "+e+"\n");
            }
        }//end of run
    }// end of class ServletThread
Avatar of Mick Barry
Adding a thread won't help you. You'll need to send "Register" and "Hello" as seperate http requests.
Once the servlet has recieved register and sent the reply that connection is closed.
You cannot do full duplex communications over http, it is a simple request/response protocol.
Hi,

If yo need to keep some state between servlet calls, you can use session for that purpose. Below is form-based servlet sample. For applet you need to simplify request-response content.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com

package ee.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class SessionServlet extends HttpServlet {

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request,response);
    }


    /**
     * Respond to a GET request for the content produced by
     * this servlet.
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are producing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
      throws IOException, ServletException {

     response.setContentType("text/html");
     PrintWriter writer = response.getWriter();

     writer.println("<html>");
     writer.println("<head>");
     writer.println("<title>Session State Test</title>");
     writer.println("</head>");
     writer.println("<body bgcolor=white>");

     String command = request.getParameter("command").trim();
     Object state = request.getSession().getAttribute("state");
     writer.println("Command: "+command+"<br>");
     writer.println("Input State: "+state+"<br>");
     if( command != null && command.length() > 0){
            if( state == null ){
                if( "Register".equals(command) ){
                    request.getSession().setAttribute("state","registered");
                    writer.println("Register OK<br>");
                }
                else{
                    writer.println("Invalid Command<br>");
                }
            }
            else if( "registered".equals(state) ){
             if( "Hello".equals(command) ){
                   writer.println("Hello! How are you?<br>");
             }
             else if( "Bye".equals(command) ){
                   writer.println("See you later<br>");
                 request.getSession().removeAttribute("state");
             }
                else{
                    writer.println("Invalid Command<br>");
             }
            }
     }
     writer.println("Output State: "+request.getSession().getAttribute("state")+"<br>");

     writer.println("<form action=\"\" method=\"POST\" >"
         +"Command: <input name=\"command\" type=\"text\" ><br>"
         +"<input type=\"submit\" value=\"Send\"></form>" );

     writer.println("</body>");
     writer.println("</html>");

    }
}


Avatar of icefire

ASKER

The target of my program is

1. Use servlet as a server which can be visited by the same applet running on more than one PCs.  

2. The applets can communicate with the servlet not according to request/respons pattern, ie both the servlet and the applets can start the sending of message.

Is there exist a reasonable design?  
ok - then after the applet calls the servlet they can use plain socket connection. But I don't think that this makes much sense. Why do you need exactly a servlet? You can create your own application that listens for applet conenctions
ASKER CERTIFIED SOLUTION
Avatar of Igor Bazarny
Igor Bazarny
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of icefire

ASKER

Thanks a lot.