1

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();
user3125693
  • 870
  • 11
  • 24

2 Answers2

1

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.

Community
  • 1
  • 1
Amila
  • 5,195
  • 1
  • 27
  • 46
  • My POJO implements the serializable interface. I just wanted to be able to send a request through JavaScript AJAX with this POJO attached. Is there an easy way for me to do that? I can't seem to find it anywher eonline – user3125693 Sep 22 '15 at 16:59
0

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.

Kevin Hooke
  • 2,583
  • 2
  • 19
  • 33