Hello,

I was wondering if there is some sort of Java code that can get information from a webite ONLY when it needs to.

To better explain (by using an example) let's say I have a slow webpage (minimal traffic) where a user clicks a button and Java opens my DVD drive (Cause why not :P ). The easy way would be to read a file from the website every x seconds, but I don't want that because that would drive bandwidth and all that up (in this example remember, it's a slow webpage).

I want to know if I can get Java to NOT loop through a file every so often, but still open that DVD drive when a user clicks that button.

I would prefer if the webpage side was in PHP, if that is possible.

If the above is not possible, is it possible for Java to read when a file was last updated? If so, how? (I don't want to use a looping method, but with the "last updated" info I can choose whether to read the file or not, reducing some usage)

Please and thank you :)

--Turt2Live

Dani AI

Generated

Following ’s scenario and ’s pointer about using file timestamps, here are practical, low-bandwidth ways to trigger a client-side action only when needed — and what to watch out for.

One clean pattern is server‑push: the Java client opens a connection and the PHP endpoint holds that request until an event happens (long polling). When a user clicks the webpage button the server writes a small “flag” and the waiting request returns immediately. Long polling is simple to implement in PHP and uses almost no bandwidth while idle. For many clients or higher scale, use WebSockets (bi‑directional) or Server‑Sent Events (uni‑directional) instead.

Minimal example (concept):

<?php
// event.php - long poll: wait up to ~50s for a flag file, then respond
$start = time();
while ((time() - $start) < 50) {
    if (file_exists('/tmp/open_dvd.flag')) {
        echo "OPEN\n";
        unlink('/tmp/open_dvd.flag');
        flush();
        exit;
    }
    sleep(1);
}
http_response_code(204); // no content, client should reconnect
// Java client: blocking GET; on 200 response act and then reconnect
URL u = new URL("https://your.site/event.php");
HttpURLConnection c = (HttpURLConnection)u.openConnection();
c.setReadTimeout(60000); // server holds for ~50s
if (c.getResponseCode() == 200) {
    // read small response and act (open the drive)
}
c.disconnect();

Notes and cautions:

  • The server cannot initiate a TCP connection to a client behind NAT/firewall; the client must connect out and keep a socket open.
  • Long polling is easy but ties up server connections; WebSockets are more efficient for many simultaneous clients (PHP can use libraries like Ratchet, or run a small Node/Java socket server).
  • If you only need cheap checks, use conditional HTTP (HEAD / If‑Modified‑Since or ETag) to avoid downloading content when unchanged.
  • Opening hardware on the user’s machine is a privileged action: don’t rely on unsigned applets. Use a locally installed, signed helper app (or custom protocol handler) and require explicit user consent.

These approaches avoid continuous tight polling while giving near‑instant response when the button is clicked.

Recommended Answers

All 2 Replies

I don't know if your first "solution" will be possible or not... But I do know that there is a method of the File class called "lastModified()".

The lastModiied method returns a long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs

Like:

long lastUpdated = myFile.lastModified();

Hope this helps!

It does, thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.