How can we tell jQuery to access a Java arraylist in JavaServlets?

I am using Java servlets and JSP in my web application. My question is that how can i tell jQuery to access a java arraylist. For example i want to show a list of books in my page and i am getting list of those books using java servlet. Now i want to tell jQuery that if a specific button is clicked then show these books in that page. How can i do that? Or is there any other way to do that? Thanks in advance.

Answer

jQuery is a client side framework, it can not access the arraylist. Servlets/JSP are server side. When jQuery sees the page, its just plain html.
What you can do is, convert your arraylist to JSON, and then output the json string in your JSP. jQuery can use the json string to display data on page.

You can have a look at http://code.google.com/p/google-gson/, which is one of the best Java-JSON library available.

Leave a Reply

Your email address will not be published. Required fields are marked *