Wednesday, June 28, 2006

That Dang URLConnection Class Bit Me Again

Over the past couple days, I've been reinventing RPC mechanisms by making use of passing serialized java object to and from servlets. I ran into some complexities that I had forgotten existed when trying to use the URLConnection class. In short the trick is to not call URLConnection.getInputStream() until you are finished writing to the URLConnection's output stream. The JavaWorld article Dodge the traps hiding in the URLConnection class helped me again. Here is some example code for posting a serialized object to a servlet and retrieving an object response from the servlet: -------------------------------------------------------------------
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;

public class Client {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost:8080/myContext/myServlet");

        URLConnection servletConnection = url.openConnection();

        // inform the connection that we will send output and accept input
        servletConnection.setDoInput(true);
        servletConnection.setDoOutput(true);

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

        // Specify the content type that we will send binary data
        servletConnection.setRequestProperty("Content-Type", "application/x-java-serialized-object");

        // get input and output streams on servlet
        ObjectOutputStream os = new ObjectOutputStream(servletConnection.getOutputStream());

        // send your data to the servlet
        String s = "Some object I am sending to the server";
        os.writeObject(s);
        os.flush();
        os.close();

        ObjectInputStream is = new ObjectInputStream(servletConnection.getInputStream());

        Object o = is.readObject();
        is.close();

        String returnedValue = (String) o;
        System.out.println(returnedValue);

    }
}
------------------------------------------------------------------------
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class InsertFeature extends GenericServlet {

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

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

        String value = "Thank You";

        try {

            // read an object
            ObjectInputStream is = new ObjectInputStream(request.getInputStream());

            String receivedString = (String) is.readObject();
            is.close();
        } catch (IOException ex) {
            value = "ERROR";
        } catch (ClassNotFoundException ex) {
            value = "ERROR";
        }

        ObjectOutputStream oStream = new ObjectOutputStream(response.getOutputStream());

        oStream.writeObject(value);
    }
}

No comments: