2

I have a problem with receiving data from JavaScript into a servlet. In my page, a user can generate an array with strings (using JavaScript). I want to receive this data into my servlet. I don't have any idea how to do it. Does anyone know how to do it?

1 Answer 1

3

You have to serialise the data.

You could encode it as JSON (json2.js provides a cross browser library that will do this) and then have a Java JSON library to decode it at the other end.

Alternatively, you can use the application/x-www-form-urlencoded encoding (as used when you submit a form) of name=value&name=othervalue (make sure you encode each part).

Once the data is serialised, you have to make an HTTP request. The simplest way is to put the data in a query string and set location.href. If you want to send the data without leaving the page, then look into XMLHttpRequest, usually abstracted via a library that irons out the differences between browsers (bigger libraries that do lots of other stuff include YUI and jQuery).

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

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.