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();
3
  • Do you mean JavaScript executing in a web browser? Commented Sep 22, 2015 at 16:36
  • Yes, I wanted to send an AJAX POST request in javascript specifically Commented Sep 22, 2015 at 16:58
  • 1
    I think your only option is JSON. Commented Sep 22, 2015 at 17:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.