Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9122209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:02:44+00:00 2026-06-17T06:02:44+00:00

I am taking some data from a database via a servlet and a db

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:02:45+00:00Added an answer on June 17, 2026 at 6:02 am

    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:

    • Using java.net.URLConnection to fire and handle HTTP requests
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am importing some data from a 10Gb file to a postgres database tables
I'm taking some information from data base and i want to use it in
I am loading url into webview: WebView webview=(WebView)findViewById(R.id.webview); webview.loadUrl(url); It's taking some time to
So I had a thread that was taking care of some network stuff for
I am taking a software engineering course and we use Java to implement some
I have some C# class libraries, that were designed without taking into account things
i'm having some problems using a custom made function to search specific data from
I'm currently writing an iPhone application which gets some data from the user and
I'm working on a solution to make certain data from a large database available
I have a database that takes in data from a WYWSWIG enabled textarea, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.