1

I am taking some data from a database via a servlet and a db handler java class and hosting it at a url. Since the database is changing I'm taking care only to host the changes rather than the entire db data.

I'm getting the required functionality by a browser i.e after every (manual) reload, I'm getting the data as required by me,

1. at the first page load, entire data gets displayed.
2. at subsequent reloads, I get either null data if there is no change in the database, or the appended rows if the database extends. (the database can only extend).

But then in a java program, I'm not getting the same functionality. The java program using HttpUrlConnection.

This is the code for the java client for servlet...

public class HTTPClient implements Runnable {

private CallbackInterface callbackinterface;
private URL url;
private HttpURLConnection http;
private InputStream response;
private String previousMessage = "";

public HTTPClient() {
    try {
        url = new URL("http://localhost:8080/RESTful-Server/index.jsp");
        http = (HttpURLConnection) url.openConnection();
        http.connect();
    } catch (IOException e) {
    }
}

@Override
public void run() {
    while (true) {
        try {
            String currentmessage = "";

            response = http.getInputStream();
            if (http.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader buffread = new BufferedReader(new InputStreamReader(response));
                String line;

                for (; (line = buffread.readLine()) != null;) {
                    currentmessage += line;
                }
                if ((!currentmessage.equals(previousMessage)
                        || !previousMessage.equals(""))
                        && !currentmessage.equals("")) {
                    //this.callbackinterface.event(currentmessage);\
                    System.out.println(currentmessage + "\t" + previousMessage);
                }
                previousMessage = currentmessage;

                Thread.sleep(2500);
            } else {
                throw new IOException();
            }
        } catch (IOException | InterruptedException e) {
            System.err.println("Exception" + e);
        }

    }
}

The shown class is a thread which read the connections every 2.5 s. If it gets something significant in the getline(), it will issue a callback to a worker method, which takes care of remaining things.

I am thinking the issues is because of the class variable conn, and that reload as in the browser is not getting replicated..

Any idea how to do this?

Swapnil
  • 7,762
  • 4
  • 34
  • 56
Vineet Menon
  • 672
  • 2
  • 8
  • 24

1 Answers1

3

You're basically connecting (requesting) only once and trying to read the response multiple times, while it can be read only once. You basically need to create a new connection (request) everytime. You need to move the creation of the connection by url.openConnection() to inside the loop. The line http.connect() is by the way superfluous. You can safely omit it. The http.getInputStream() will already implicitly do it.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • but then, in the servlet I have made the db reader a session object, by which what I want is to provide individualised db readers to each connection. If now I put the connect() inside run(), then I cannot achieve that(?).. – Vineet Menon Jan 12 '13 at 14:47
  • 1
    Just pass session cookie back on subsequent request. See also the "See also" link, under the section "Maintaining the session". Reusing a request isn't the same as maintaining the session. By the way, having a "DB reader" in the session is in turn also a design smell, but that's a different subject. – BalusC Jan 12 '13 at 14:49
  • okay, thanks for the suggestion.. I will give it a try...and about that db reader (per session basis) , can you elaborate please?? – Vineet Menon Jan 12 '13 at 15:04
  • This answer may give some pointers: http://stackoverflow.com/questions/9428573/is-it-safe-to-use-a-static-java-sql-connection-instance-in-a-multithreaded-syste/9431863#9431863 It's not exactly your situation, but it at least explains things to watch out. – BalusC Jan 12 '13 at 15:05