6

I am using Ajax in my JavaScript and that sets an array of objects of following class in a response:

public class RetrieveTempSecVersions {

    private String templateName;
    private int[] versionNumber;

    public String getTemplateName() {
        return templateName;
    }

    public void setTemplateName(String templateName) {
        this.templateName = templateName;
    }

    public int[] getVersionNumber() {
        return versionNumber;
    }

    public void setVersionNumber(int[] i) {
        this.versionNumber = i;
    }
}

using:

aoRes.setContentType("text/xml");
aoRes.setHeader("Cache-Control", "no-cache");
aoRes.getWriter().write(template[]);

Now I want to read all the elements from template array and its details into my JavaScript.

Can anyone help me in how to do that?

3 Answers 3

3

Have you considered using Google Web Toolkit? With GWT you have a number of choices for sending objects between client and server, including GWT RPC, RequestFactory, and JSON.

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

Comments

2

You need to transform your array of Java objects into something JavaScript understands. There are two obvious choices : XML (AJAX means Asynchronous Javascript And XML), or JSON. JSON is probably easier and more lightweight.

All the AJAX JavaScript libraries (JQuery, etc.) have good support for JSON. See http://api.jquery.com/jQuery.getJSON/ for example.

There are also several Java APIs to transform Java objects into JSON (look at GSON for example).

You'll have to modify the content type of the response : it's not text/html, but application/json if you use JSON.

Comments

1

As already said jQuery is very good for ajax manipulation.

Another good framework I liked to use was DWR: https://github.com/directwebremoting/dwr

It is quite lighter than jQuery (because only focused on Ajax calls and not components) and very easy to understand.

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.