user3125693
user3125693

Reputation: 950

Sending a Java object through javascript AJAX

Currently I have a Java servlet that takes in a serialized POJO through the request. It works when I send a request from a Java client to the servlet. My question is: is there a way to make such a request through JavaScript using AJAX passing the Java object directly as opposed to converting it to a JSON first? This is how my server receives the Java object

ObjectInputStream inData = new ObjectInputStream (request.getInputStream());
SomeClass mUser = (SomeClass) inData.readObject();

Upvotes: 1

Views: 1681

Answers (2)

Kevin Hooke
Kevin Hooke

Reputation: 2621

No, you have to convert the representation of the Java class instance into something that JavaScript can parse and handle. Java != JavaScript, the two are different technologies.

There are many libraries/frameworks that can help you with the conversion back and forth though. Rather than object serialization, look at using JAX-RS for your serverside resource, and a mapping library like Jackson to map JSON to Java and back.

Upvotes: 0

Amila
Amila

Reputation: 5213

A Java object is just an instance of a class in memory. When you pass data from client to server, it has to be serialized one way or another.

Upvotes: 1

Related Questions