1

I have a client-server program that are using servlets to communicate with each other, each of them is sending an object (of a class that I defined) to the other. Can I send it directly using the setContentType(myClass) in the servlet? Or do I need to something completely different? I couldn't find a way to do it.

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
Shai Zarzewski
  • 1,528
  • 3
  • 18
  • 31

2 Answers2

2

You need to serialize the object at server side, send it as an array of bytes, or encoded as a text (using base64 for example), and than deserialize it at the client side.

Antonio
  • 11,629
  • 11
  • 60
  • 88
  • how can I serialize an image file? in my object U have a few strings and lists and an image – Shai Zarzewski Jan 26 '13 at 11:17
  • Yes, you can uses standard [java serialization API](http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serialTOC.html). But this will only work if client and server has equal JRE version. In case you need support for different versions of JRE, you can [serialize to XML](http://stackoverflow.com/questions/35785) – Antonio Jan 26 '13 at 12:45
0

There is one more alternative approach: use WebServices. You can get to know more about them at Apache AXIS website.

Antonio
  • 11,629
  • 11
  • 60
  • 88