Search code examples
javaweb-servicesappletclient-serverrmi

Applet - server communication, how can I can do it?


I have an applet and I must send a request to a web application to get data from the server that is in a database. I am working with objects and it is very useful that the server responses me with objects!!

How an applet can communicate with a server?

I think web services method, RMI and... make me happy, but which is the best and reliable?


Solution

  • As long as its only your applet communicating with the server you can use a serialized object. You just need to maintain the same version of the object class in both the applet jar and on the server. Its not the most open or expandable way to go but it is quick as far as development time and pretty solid.

    Here is an example.

    Instantiate the connection to the servlet

    URL servletURL = new URL("<URL To your Servlet>");
    URLConnection servletConnect = servletURL.openConnection();
    servletConnect.setDoOutput(true); // to allow us to write to the URL
    servletConnect.setUseCaches(false); // Write the message to the servlet and not from the browser's cache
    servletConnect.setRequestProperty("Content-Type","application/x-java-serialized-object");
    

    Get the output stream and write your object

    MyCustomObject myObject = new MyCustomObject()
    ObjectOutputStream outputToServlet;
    outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
    outputToServlet.writeObject(myObject);
    outputToServlet.flush(); //Cleanup
    outputToServlet.close();
    

    Now read in the response

    ObjectInputStream in = new ObjectInputStream(servletConnection.getInputStream());
    MyRespObject myrespObj;
    try
    {
        myrespObj= (MyRespObject) in.readObject();
    } catch (ClassNotFoundException e1)
    {
        e1.printStackTrace();
    }
    
    in.close();
    

    In your servlet

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
      MyRespObject myrespObj= processSomething(request);
      response.reset();
      response.setHeader("Content-Type", "application/x-java-serialized-object");
      ObjectOutputStream outputToApplet;
      outputToApplet = new ObjectOutputStream(response.getOutputStream());
      outputToApplet.writeObject(myrespObj);
      outputToApplet.flush();
      outputToApplet.close();
    }
    
    private MyRespObject processSomething(HttpServletRequest request)
    {
      ObjectInputStream inputFromApplet = new ObjectInputStream(request.getInputStream());
      MyCustomObject myObject = (MyCustomObject) inputFromApplet.readObject();
      //Do Something with the object you just passed
      MyRespObject myrespObj= new MyRespObject();
      return myrespObj;
    }
    

    Just remember that both Objects that you are passing need to implement serializable

     public Class MyCustomObject implements java.io.Serializable
     {