1

I am asking because I have only seen java beans used with a framework like struts or JSF.

Is it possible to send and access a java bean over an AJAX request?

Servlet myServlet creates and fills a java bean instance, and sets it in the request scope. An already loaded jsp/html page uses AJAX to request data from myServlet. Can this bean be accessed in any way? After thinking for a while, I have come to accept that this cannot be done.

If it can't be done, what would be the best practice when trying to transmit data from a model (i.e. user information from a database) asynchronously to a client when using Tomcat/Servlets and JSP?

GCon
  • 1,397
  • 3
  • 16
  • 33

2 Answers2

1

It's technically possible if you serialize the javabean to a byte array or even a base64 encoded string using the usual Java Serialization API.

But how would it ever make sense to use a proprietary format to transfer data around? How would non-Java clients (e.g. JavaScript!) ever be able to consume the serialized Java object? These days XML, JSON and even CSV are much more widely supported and accepted. Practically every self-respected programming language has tools to easily convert between XML/JSON/CSV and the model as definied in the programming language in question. E.g. Java has JAX-RS API to easily convert between javabeans and XML or JSON. JavaScript has —obviously— builtin support for JSON (guess what "JS" in JSON stands for).

To learn and play around with the basic concept, head to this answer: How to use Servlets and Ajax?

To learn about the advantages of JAX-RS over servlet, head to this answer: Servlet vs RESTful

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you very much for you answer. I had a feeling this would be the case. I will probably use XML or JSON for the data. I will read your links and hopefully clarify things more. Thanks again! – GCon May 16 '13 at 15:50
  • You're welcome. If you reach only JavaScript based clients, JSON is preferable as it's more compact and immediately parseable. However if you reach more different clients, supplying multiple different formats is preferable. This is also where JAX-RS excels. – BalusC May 16 '13 at 15:54
1

You can still use struts or jsf as you would normally to construct markup(html). And then consume the markup that was constructed via ajax and then append to the dom. If you are familiar with jQuery, something like jQuery('#selector').load('actionUrl.action'); But if you are looking to examine a java bean, then you will have to serialize it to xml or json. If you are using a web framework like struts2 or spring, there is likely a mechanism for doing this serialization for you. If you want to edit the bean you will have to serialize, then edit the serialized bean, and then deserialize back to the java bean.

Sumit
  • 1,661
  • 1
  • 13
  • 18
  • Thank you for your answer. The project was academic, and JSF wasn't __explicitly__ requested. We are experimenting with it after all, trying to see if learning it outweighs using simple JSON. – GCon May 21 '13 at 09:54