Edit Edit

Java Servlet FAQ


How does an applet communicate with a servlet?


There are several ways for an applet to communicate with a servlet:

1) HTTP GET Method - easy to pass simple parameter types like strings to the servlet

The applet can send a GET request to the servlet by opening a servlet URL with a query string. For example,

// servlet URL with a name paremeter in a query string
URL url = new URL(getCodeBase(), "/servlet/MyServlet?nameparam=John+Smith");

// open a connection to the servlet
URLConnection servletConnection = url.openConnection();
servletConnection.setDoInput(true); // true, if we get data back

// get the input stream from the servlet
InputStream in = servletConnection.getInputStream();

// read the response from the servlet
...

2) HTTP POST Method - useful for passing a large amount of text or binary data to the servlet

The applet can send a POST request to the servlet by opening a servlet URL and then writing the stream of data to the servlet. For example,

// servlet URL
URL url = new URL(getCodeBase(), "/servlet/MyServlet");

// open a connection to the servlet
URLConnection servletConnection = url.openConnection();

// prepare for both input and output
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);

// don't use a cached version of URL connection
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);

// set the content type to indicate that we're sending binary data
servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");

// get input and output streams on servlet
InputStream in = servletConnection.getInputStream();
OutputStream out = servletConnection.getOutputStream();

// send your data to the servlet
...

// read the response from the servlet
...

3) Object Serialization - PROS: easy to pass complex data to or from a servlet, CONS: only works if the browser running your applet supports JDK 1.1 or later

The applet can use Java serialization to send and/or receive class objects to/from the servlet. The classes you are passing must implement the java.io.Serializable interface.

The applet invokes the servlet directly and passes the serializable class object in an object stream:

// servlet URL
URL url = new URL(getCodeBase(), "/servlet/MyServlet");

// open a connection to the servlet
URLConnection servletConnection = url.openConnection();

// prepare for both input and output
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);

// don't use a cached version of URL connection
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);

// set the content type to indicate that we're sending binary data
servletConnection.setRequestProperty ("Content-Type", "application/octet-stream");

// get input and output streams on servlet
InputStream in = servletConnection.getInputStream();
OutputStream out = servletConnection.getOutputStream();

// data to pass to the servlet
DataIn data_in = new DataIn();

// create an output stream
ObjectOutputStream oos = new ObjectOutputStream(out);

// write the serialized data object to the output stream
oos.WriteObject(data_in);
oos.flush();
oos.close();

// create an input stream
ObjectInputStream ois = new ObjectInputStream(in);

// now read the object returned from the servlet
DataOut data_out = (DataOut) ois.ReadObject().

The declarations for the classes that are passed to and from the servlet would look something like this:

public class DataIn implements Serializable
{
    // data to pass to servlet is declared here
    ...
}

public class DataOut implements Serializable
{
    // data to get from servlet is declared here
    ...
}

The servlet reads the serialized object when it processes the POST request:

public void doPost(HttpServletRequest req, HttpServletResponse res)
                   throws ServletException, IOException
{      
    // get the input stream from the applet
    InputStream in = req.getInputStream();

    // create an object input stream
    ObjectInputStream ois = new ObjectInputStream(in);
   
    // read the serialized data object
    DataIn data_in = (DataIn) ois.readObject();      
    in.close();

    // do some processing with the input data
    ...

    // create some data object from the input data
    DataOut data_out = new DataOut();

    // set the content type
    res.setContentType("java-internal/" + DataOut.class.getName());

    // get the output stream
    OutputStream out = res.getOutputStream();

    // create an object output stream
    ObjectOutputStream oos = new ObjectOutputStream(out);

    // write the serialized output object
    oos.WriteObject(data_out);
    oos.flush();
    oos.close();
}

4) Raw Sockets - PROS: bidirectional communication, CONS: most firewalls do not allow raw socket connections

5) RMI - PROS: object-oriented, works with firewalls, CONS: most browsers do not support RMI and and those that don't require a plug-in to be installed to support RMI

6) CORBA - PROS: CORBA objects can be written in any language



Last updated 12/24/1999 07:35:24 PM