0

My client does not implement the HttpServlet interface. It connects to an HttpServlet running remotely

 url = new URL("http://tomcat-location:8180/myContext");

This is what it uses to send messages to the servlet. But how can it get back responses? When I try to read from this location, I am reading the content of the specified page. Maybe my whole approach is wrong and this is not how the client and the servlet should talk to each other? How can I make them talk in a simple way? It seems that using the URL, they are communicating by posting and reading on that page. Is there any other way they can talk without writing on the page? Thanks

Trup
  • 1,635
  • 13
  • 27
  • 40
  • Do you want to read what is returned by `http://tomcat-location:8180/myContext`? – davidbuzatto Aug 17 '12 at 03:42
  • Yes, I want to read what the servlet response is. Basically the servlet manages a conversation between the 2 clients: client1 says something to servlet, then servlet forwards this to client2, and so on, in a regular, alternating way. – Trup Aug 17 '12 at 03:47
  • There is no HttpServlet interface. It's an abstract class. Clients never extend it. Servlets do. Servlets and HTTP URLs communicate via GET and POST requests. Your question remains totally unclear. – user207421 Aug 17 '12 at 09:48

1 Answers1

0

Try this:

public static String getURLData( String url ) {

    // creates a StringBuilder to store the data
    StringBuilder out = new StringBuilder();

    try {

        // creating the URL
        URL u = new URL( url );

        // openning a connection
        URLConnection uCon = u.openConnection();

        // getting the connection's input stream
        InputStream in = uCon.getInputStream();

        // a buffer to store the data
        byte[] buffer = new byte[2048];


        // try to insert data in the buffer until there is data to be read
        while ( in.read( buffer ) != -1 ) {

            // storing data...
            out.append( new String( buffer ) );

        }

        // closing the input stream
        in.close();            

        // exceptions...  
    } catch ( MalformedURLException exc )  {

        exc.printStackTrace();

    } catch ( IOException exc ) {

        exc.printStackTrace();

    } catch ( SecurityException exc ) {

        exc.printStackTrace();

    } catch ( IllegalArgumentException exc ) {

        exc.printStackTrace();

    } catch ( UnsupportedOperationException exc ) {

        exc.printStackTrace();

    }

    // returning data
    return out.toString();

}

If you need to work with a proxy you will need to do some more work, since you will need to authenticate. Here you can read something about it: How do I make HttpURLConnection use a proxy?

If you want a client that works like a browser, you can try the HttpClient from Apache HttpComponentes

Now, if you need a behavior where the server notifies the client, so you will need to use another approaches, like creating your own server and working with sockets. If you work with a regular browser you can use websockets, but it seems that is not your case.

Community
  • 1
  • 1
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
  • My intent is for the servlet to manage a conversation between the 2 clients: client1 says something to servlet, then servlet forwards this to client2, and so on, in a regular, alternating way. Will your approach work for this purpose? – Trup Aug 17 '12 at 03:51
  • My approach will only get the data. If you want a client that works like a browser, you can try the HttpClient from [Apache HttpComponentes](http://hc.apache.org/httpcomponents-client-ga/) – davidbuzatto Aug 17 '12 at 03:54