P.JAYASRI
P.JAYASRI

Reputation: 358

how can i pass java object from server side to client side

can i pass java POJO class object to client side .
for example user send request to server "/user". server should send response as User.java object

User.java class is

public class User {

private String name = null;
private String education = null;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getEducation() {
    return education;
}
public void setEducation(String education) {
    this.education = education;
}

}

Upvotes: 0

Views: 1302

Answers (4)

Pasupathi Rajamanickam
Pasupathi Rajamanickam

Reputation: 2052

I had the same requirement, I used servlet to do that. If you can use servlet, you can take this reference.

Servlet side

ObjectInputStream objectStream = new ObjectInputStream(request.getInputStream());
User user = (User) objectStream.readObject();
objectStream .close();

Client side

User user = new User();
urlConnectionToTarget.setRequestMethod("POST");
urlConnectionToTarget.setDoOutput(true);
urlConnectionToTarget.setDoInput(true);
urlConnectionToTarget.setRequestProperty("Content-Type", "application/octet-stream");
urlConnectionToTarget.connect();
ObjectOutputStream servletObjectStream = new ObjectOutputStream(urlConnectionToTarget.getOutputStream());
servletObjectStream.writeObject(user);
servletObjectStream.flush();
servletObjectStream.close();

Upvotes: 1

Dunxton
Dunxton

Reputation: 408

There are two simple ways that I can think of:

  1. Use JAXB to convert the User object into an xml and back to transfer the data between client and server.
  2. Use GSON/Jackson to do the same with a JSON.

Either ways the XML/JSON will directly map to your Objects and are fairly easy to implement.

There are other ways but I assume that you are using a web service and these are best suited for it.

Upvotes: 1

ravin
ravin

Reputation: 11

There are many ways:

  1. You can use any of the JSON,XML,SOAP formats.

  2. Or you can use applet

  3. Rmi if using standalone java client.

Upvotes: 1

Clyde D'Cruz
Clyde D'Cruz

Reputation: 2065

As mr.icetea suggested you can serialize the java object to json and then pass it . You can do the serialization/deserialization using the jackson library : http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

Upvotes: 1

Related Questions