【问题标题】:nearest equivalent of a webpage reload in javajava中最接近网页重新加载的等价物
【发布时间】:2012-12-26 23:43:19
【问题描述】:

我正在通过 servlet 和 db 处理程序 java 类从数据库中获取一些数据,并将其托管在 url。由于数据库正在发生变化,我只注意托管更改而不是整个数据库数据。

我正在通过浏览器获得所需的功能,即每次(手动)重新加载后,我都会根据我的要求获得数据,

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).

但是在 java 程序中,我没有得到相同的功能。使用HttpUrlConnection的java程序。

这是 servlet 的 java 客户端的代码...

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);
        }

    }
}

显示的类是一个线程,它每 2.5 秒读取一次连接。如果它在getline() 中获得了重要的信息,它会向工作人员方法发出回调,该方法负责处理剩余的事情。

我认为问题是因为类变量 conn,并且在浏览器中重新加载没有被复制..

知道怎么做吗?

【问题讨论】:

    标签: java web-services http servlets


    【解决方案1】:

    您基本上只连接(请求)一次并尝试多次读取响应,而它只能读取一次。您基本上每次都需要创建一个新的连接(请求)。您需要将url.openConnection() 的连接创建移动到循环内部。顺便说一句,http.connect() 行是多余的。您可以放心地忽略它。 http.getInputStream() 已经隐含地这样做了。

    另见:

    【讨论】:

    • 但是,在 servlet 中,我将 db reader 设置为 session 对象,我想要通过它为每个连接提供个性化的 db reader。如果现在我将 connect() 放在 run() 中,那么我无法实现 (?)..
    • 只需在后续请求中将会话 cookie 传回即可。另请参阅“维护会话”部分下的“另请参阅”链接。重用请求与维护会话不同。顺便说一句,在会话中有一个“DB 阅读器”反过来也是一种设计味道,但这是一个不同的主题。
    • 好的,谢谢你的建议。我会试一试...关于那个数据库阅读器(每个会话),你能详细说明一下吗??
    • 这个答案可能会给出一些指示:stackoverflow.com/questions/9428573/…这不完全是你的情况,但它至少说明了需要注意的事情。
    猜你喜欢
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 2015-01-04
    • 2012-05-19
    • 1970-01-01
    • 2019-11-03
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多