Header Ads

  • Breaking Now

    More on Applet-Servlet Communication

    An applet-servlet/server communication can occur in following ways:

    Approach I:
    This approach reads ASCII and Binary data through applet and servlet communication.It makes sense when applet is talking to an arbitrary server-side program or reading the content of static Web pages.As Http is a request/response protocol when a connection is established, you send a request. The server processes the request,creates a response and sends it.And once you receive it,that is the end of the connection.An applet can read the content sent by the server by first creating a URLConnection derived from the URL of the server-side program and then attaching a BufferedInputStream to it.As Http is a request/response protocol when a connection is established, you send a request. The server processes the request,creates a response and sends it.And once you receive it,that is the end of the connection.

    This reading from server can be summarized in following steps:

    1.Create URL object where Applet is hosted
    2.Create a URLConnection from URL object from previous step
    3.Tell browser not to cache information so that each time you get fresh information from server
    4.Create input stream,the most commonly used is BufferedReader
    5.Read each line from input stream
    6.Close the input stream

    Approach II:
    This approach is based on the concept of Java Serialization.Where an object is read from applet side which has been written from server side.The steps have been summarised from both applet and server end.

    At Applet end, reading an object has all the steps same as Approach I except step 4 and 5.

    1.Create URL object where Applet is hosted
    2.Create a URLConnection from URL object from previous step
    3.Tell browser not to cache information so that each time you get fresh information from server
    4.Create ObjectInputStream object.
    5.Read datastructure from readObject() method.
    6.Close the input stream

    At Servlet end:
    1.Specify that binary content is being sent.

    String contentType ="application/x-java-serialized-object";
    response.setContentType(contentType);

    2.Create an ObjectOutputStream.
    3.Write the data structure by using writeObject() method.
    4. Flush the stream to be sure all content has been sent to the client.

    out.flush();

    Post Top Ad

    Post Bottom Ad