Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HttpURLConnection to send serialized object to a Servlet from Java class?

I want to send a serialized object from a Java class to a servlet where the servlet should retrieve and save the object as a file. I'm aware that I have to use HttpURLConnection to make a POST request to a servlet, but I don't know whether the below code is correct.

private static HttpURLConnection urlCon;
private static ObjectOutputStream out;

public static void main(String[] args) {

    Names names = new Names();
    names.setName("ABC");
    names.setPlace("Bangalore");
    URL url;
    try {
        url = new URL("http://localhost:6080/HttpClientSerializable/HttpPostServlet");
    try {
        out = (ObjectOutputStream) urlCon.getOutputStream();
        out.writeObject(names);
        urlCon = (HttpURLConnection) url.openConnection();
        urlCon.setRequestMethod("POST");
        urlCon.setDoOutput(true);
        out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
}

And in the servlet, I have the following code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ObjectInputStream in = new ObjectInputStream(request.getInputStream());
        try {
            names = (Names) in.readObject();

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        in.close();
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:/Documents and Settings/RAGASTH/Desktop/Names"));
        out.writeObject(names);
        out.close();

    }

What should I do to make it work? Also, I want the servlet to send back the object it receives as response.

Any help would be appreciated. Thanks!

like image 232
Rajath Avatar asked Oct 09 '12 11:10

Rajath


People also ask

Can we transfer a serialized object via network?

Stated differently, serialization is the conversion of a Java object into a static stream (sequence) of bytes, which we can then save to a database or transfer over a network.

How does HttpURLConnection work in Java?

HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol.

What HTTP methods are supported by a Java net HttpURLConnection?

It works for HTTP protocol only. By the help of HttpURLConnection class, you can retrieve information of any HTTP URL such as header information, status code, response code etc.

What is the difference between URLConnection and HttpURLConnection?

URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.


1 Answers

You would need to

  • Make sure the Names class implements java.io.Serializable marker interface.
  • Create an ObjectOutputStream from the servlet's outputstream as follows:

    out = new ObjectOutputStream(urlCon.getOutputStream());
    
  • On the receiver side, once you read the object from the servlet's inputstream and persist in the file, write it back to the response's output stream as follows:

    out = new ObjectOutputStream(response.getOutputStream());
    out.writeObject(names);
    out.close();
    

On the sender's side:

Names names = new Names();
names.setName("ABC");
names.setPlace("Bangalore");
URL url;
try {
    url = new URL("http://localhost:6080/HttpClientSerializable/HttpPostServlet");
    urlCon = (HttpURLConnection) url.openConnection();

    urlCon.setDoOutput(true); // to be able to write.
    urlCon.setDoInput(true); // to be able to read.

    out = new ObjectOutputStream(urlCon.getOutputStream());
    out.writeObject(names);
    out.close();

    ObjectInputStream ois = new ObjectInputStream(urlCon.getInputStream());
    names = (Names) ois.readObject();
    ois.close();
} catch (IOException e) {
    e.printStackTrace();
}
catch (MalformedURLException e1) {
    e1.printStackTrace();
}

On the receiver's side:

ObjectInputStream in = new ObjectInputStream(request.getInputStream());
try {
    names = (Names) in.readObject();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
in.close();
ObjectOutputStream out = new ObjectOutputStream(
    new FileOutputStream("C:/Documents and Settings/RAGASTH/Desktop/Names"));
out.writeObject(names);
out.close();

ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(names);
oos.close();
like image 93
Vikdor Avatar answered Sep 27 '22 18:09

Vikdor