send object using HTTPservletResponse - java

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.

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.

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

Related

Passing structure as parameter in RestApi using JAVA

I am basically trying to pass structures (complex objects) as arguments to a rest based service. Uptill now I know pass primitive types in URIs as varname=value&varname2=value.
My Question is how to pass structures containing multiple fields to the web service?
For example i have a Rest based web service that maintains employee records. Now if a particular record has 15 fields, then when using POST method how will a client be able to send those 15 values apart from send it in URL.
So a simple object Employee having name, employee id, date of joining, etc.
I am using Eclipse for developing a java client.
My webservice is wrttien in C using gSoap having httpget and httppost plugins.
Requirement is to send complex structures as parameters to the service which can do processing on it and can return desired values.
The normal solution used by most restful services I have used is to include JSON (or XML) in the body of the post message and return JSON (or XML) encoded data in the reply.
You will find support for JSON encoding and decoding in Java so it will be easy to implement.
It is also a much better solution than your current use of the URL for several reasons.

stream type sockets to exchange objects using serialization?

I am studying exam questions and I am wondering how to go about the following question
Write a Java application that uses Stream type sockets to exchange Java Objects
using object serialisation. The client side should connect to the server and send it
an Integer Object. The server should print out this value and respond to the client
with a text based response encapsulated in a String Object. The client should
receive the String Object from the server and print out this response.
I have some ideas but I am not sure how to put it all together.. I am wondering if anyone knows of a sample I could refer to or had any code themselves. Sorry I am new here and maybe i am asking too much.
Thanks alot for reasing eitherway.
You need to look up java.net.Socket and ServerSocket, and java.io.ObjectOutputStream and ObjectInputStream and Serializable, and find the Custom Networking trail of the Java Tutorial.

How can I send an instance over a socket connection?

I'm attempting to write two Java programs.
One to simulate a server, and one to simulate a client.
How could I go about sending an instance of a Response class over a socket?
The Response class represents status codes of the server connection. e.g. 404 Not Found etc
I'm not allowed to use Serialisation unfortunately.
Any advice would be greatly appreciated.
At some level Serialization must occur in order to send an object across a connection. I can only assume your comment about being not allowed to use serialization refers to not being able to use Serializable instead of a blanket prohibition of serialization(which makes no sense). A very simple method to accomplish this would be the use of a external serialization library such as gson. Gson serializes an object into a JSON string that you can transmit over your socket and then using the same library deserialize it back into an object on the other side. You can of course use any of your preferred serialization libraries with your favorite format eg. XML, json, YAML,...
You wouldn't be sending an instance of the Response class itself. When sending things over a network, client and server machines understand bytes. Your application can understand more than bytes, it can understand specific representations. For example, your server might send a JSON representation of your Response class like:
{
"response" : {
"code":404
}
}
Then your client must be able to understand what this sequence of bytes means. That's basically what a protocol is: how two machines can communicate.
Regardless of what language the server or clients are written in, the Response is an Entity. In Java you might use a Class to represent it, in C++ you might use a struct. However, both would need to know that when you are communicating with an external application.system, they would have to put it in a format that everyone understands, be it json, xml, or any other.
As for sending this through sockets, Oracle has a nice tutorial here. You get the OutputStream
from the socket and start writing your representation.

marshal/unmarshal a java method

I have a java class that contain a method with some logic control . This class is avaible as output via a webservice method to a java swing client .
My question sounds crazy but is it possible to marshal/unmarshal the method to make it available to the client when he generate the class from the wsdl file ?
Thanks.
ps:I'm using Metro/jax-ws and glassfish 3.1.1.
You mean so you can send some logic down to the client to be executed there?
I'm afraid the answer is no - this is not how web services work.
All the logic is performed on the server side.
Why do you wish to do this?
Why not provide this class (your VO) to the client so both the server and client are marshalling/unmarshalling the same class? Then both can use the your method...
Marshalling" refers to the process of converting the data or the objects into a byte-stream, and "unmarshalling" is the reverse process of converting the byte-stream back into their original data or object.
The conversion is achieved through "serialization".
The purpose of the "marshalling/unmarshalling" process is to transfer data between the RMI systems.
For more http://ws.apache.org/old/jaxme/manual/ch02s02.html

Sending object via socket, but different language client, how to make the object serialize?

I have a Java Server that sending the java serializable object to my client, and receive java serializable object for execution. If my client is also java written, which is nice, that allow me to do communication within any problems.
But now, I would like to extend my programme to not only java client, the client may be written in C, objective C, python or php. So, I would like to do something to "convent" to client request to a java object, and send back to Server. The convent process, I can use the JSON to receive, and construct a Java object to the Server, but I also need a layer that convert back the Java object to JSON to the client.
My Question is except make a JSON-Java Translation layer, is there any other ways to do so? Also, we can afford to change some code in server side, but we must use Java as our primary language for that. Any suggestions? Thanks.
I use Netty API for designing my protocol and it is quite quick to do so if you can understand a NIO-like Byte and Buffer API.
It is design to work with a concept of Encoder and Decoder that could fit your need, there are a lot of default implementation of Encoder and Decoder for zipping, using ssl...
The problem you have seems to looks like this one:
JBoss Netty with JSON
I don't know JSON very well but most of the time is could also be quick and easy to design your own protocol.
Do you need a generic Serialization process for any kind of Object or do you simply need to serialize some String and primitive types (Integer, Short, Float..etc)?
In the case of simple objects it is easy and a lot faster to do the wrapper by yourself.
If objects are quite simple, and I would guess this is the case, your need it to design your own "protocol" specification meaning how to turn each Object into a sequence of primitive types, String and arrays. Than it should be quite easy to write both the Encoder and the Decoder in each language.
Good luck
There are other libraries designed for this, like protocol buffers and thrift.
http://thrift.apache.org/
http://code.google.com/p/protobuf/

Categories