Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return data from Servlet to Java Client

Tags:

java

servlets

Hi i have a problem with returning data from Servlet to Java Client. This is a first time that i use a servlet. All examples that i saw on the web return data to an HTML page but i want make a Server-Client software where Server do something and return a String List.

How can i return from a GET/POST method an Array to a Client? What do i set in setContentType? I didnt understand how can i put in response the information that i want (like int , array , String) and return to the Client.

If someone could make an example where a Java Client make a POST request and a Servlet return to him a Array or ArrayList i would be very happy.

like image 353
Strom Avatar asked Oct 20 '11 14:10

Strom


People also ask

How can we read client data using servlet?

We need to use either doGet() or doPost() method in the servlet class to get the information from the browser based on the method specified in the form.

How can a servlet get information about the client machine?

Getting Information About the Client MachineThe information comes from the socket that connects the server to the client, so the remote address and hostname may be that of a proxy server. An example remote address might be "192.26. 80.118" while an example remote host might be "dist.engr.sgi.com" .

What is used for get () method in servlet?

The GET method is the default method to pass information from browser to web server and it produces a long string that appears in your browser's Location:box. Never use the GET method if you have password or other sensitive information to pass to the server.

What method can be used to retrieve all the parameter values being sent as part of the request by the client?

If there’s any chance a parameter could have more than one value, you should use the getParameterValues( ) method instead. This method returns all the values of the named parameter as an array of String objects or null if the parameter was not specified.


2 Answers

You are running into the problem of serialization. Serialization is where you convert some data into a format that can be transmitted. There are several ways of doing this, some are mentioned in other answers.

I would suggest using JSON as your format. You can get a nice JSON library for java from json.org. Then you can simply create a JSON array with the library and write it to the servlet's OutputStream.

public void service(ServletRequest req, ServletResponse res) {
    final JSONArray arr=new JSONArray();
    for (String s : this.myListOfStrings){
        arr.put(s);
    }
    //Here we serialize the stream to a String.
    final String output = arr.toString();
    res.setContentLength(output.length());
    //And write the string to output.
    res.getOutputStream().write(output.getBytes());
    res.getOutputStream().flush();
    res.getOutputStream().close();
}

Now from your client, you can make the request and get back your ArrayList like so:

public ArrayList<String> contactServer(){
    final URL url = new URL(serverURL);
    final URLConnection connection=url.openConnection();
    connection.setDoOutput(true);
    /*
     * ...
     * write your POST data to the connection.
     * ...
     */
    final BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    final char[] buffer=new char[Integer.parseInt(connection.getHeaderField("Content-Length"))];
    int bytesRead=0;
    while (bytesRead < buffer.length){
        bytesRead += br.read(buffer, bytesRead, buffer.length - bytesRead + 1);
    }
    final JSONArray arr = new JSONArray(new String(buffer));
    final ArrayList<String> ret = new ArrayList<String>(arr.length());
    for (int i=0; i<arr.length(); i++) {
        ret.add(arr.get(i));
    }
    return ret;
}
like image 194
Jack Edmonds Avatar answered Oct 12 '22 07:10

Jack Edmonds


You seem to need a RESTful service over http. You should choose the way you want to serialize your objects. The typical choice is JSON - you serlialize the object to JSON and write it to the response (with Content-Type set to application/json

There are frameworks that do that - take a look at Spring MVC or Jersey/Resteasy

If you want something more low-level, you can use RMI or sockets directly, without using a servlet. Servlets are aimed to respond to HTTP requests, which can only transmit textual data.

like image 37
Bozho Avatar answered Oct 12 '22 08:10

Bozho