Pinio
Pinio

Reputation: 19

An array from JavaScript to servlet

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?

Upvotes: 2

Views: 828

Answers (1)

Quentin
Quentin

Reputation: 943209

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

Upvotes: 3

Related Questions