Java Applet Browser Refresh

I need a java applet that checks a database for changes. If there are changes in the database the applet needs to just refresh the asp page that it is hosted in?

I can't use a meta refresh because they only want to update the asp page if the data in the database has been changed. The applet should check the database every 10 seconds for an update.

Or could someone show me how to make a java application push data to a java applet?
LVL 1
onebadboyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
bobbit31Connect With a Mentor Commented:
why don't you have the applet hosted on a separate page and then query the asp page for db changes and receive the data from the asp page? (ie. have the asp page output it's result)

ie:
import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import java.net.URL;
import java.net.URLConnection;
import java.net.InetAddress;
import java.io.*;

public class GetChanges extends java.applet.Applet implements Runnable {
   
     private Thread t;
     private String sUrl;
     private TextArea ta;
     
     public void init()
     {
          t = new Thread(this);    
          t.setPriority(Thread.MIN_PRIORITY); // be nice
          t.start();
     }

     public void destroy() {
          // stop the thread
          t = null;
     }


     // this is the routine that will query your asp page and disp results
     public void run() {
         
          Thread thisThread = Thread.currentThread();
         
          while (t == thisThread) {
               BufferedReader in = null;
               
               try {
                   
                    // assuming the asp page the checks the db for changes
                    // is in the same folder as the page that holds the applet
                    URL myURL = new URL (getDocumentBase(), "CheckDB.asp");
                    URLConnection con = myURL.openConnection();
     
                    in     = new BufferedReader(new InputStreamReader(con.getInputStream()));
     
                    String s;
                    String response="";
                             
                // read in result
                while ((s = in.readLine()) != null) {
                    response += s;
                }
               
                 // response holds the output from CheckDB.asp
                                   
                  // sleep for 10 seconds
                t.sleep(10000);                
               } catch (IOException ioe) {
                    // error handling
               } catch (InterruptedException ie) {
                    // error handling    
               } finally {
                    // close stream
                    try {
                         if (in != null) {     in.close(); }
                    } catch (IOException ie) {
                         // more error handling
                    }
                }
          }
     }          
}

if there were no changes, just don't output anything from the asp page.
0
 
onebadboyAuthor Commented:
Here's the problem domain.

We have page that our staff writers post comments on the market. They talk about what they would play, what they would hold. The market in general. These are big articles usually about a 6 sentence paragraph.

The users are complaining about two things.

One: that the Meta refresh is taking to long.
ie: They are missing updates because we are refreshing every 2 minutes or so.

Two: That the page is refreshing but there is no new content so they are losing there place on the page for no reason.

problem one wouldn't be a problem if we didn't have the second issue.

Is this solution still going to work? Can you give me a sample of outputing the data from an asp script
0
 
onebadboyAuthor Commented:
Here's the problem domain.

We have page that our staff writers post comments on the market. They talk about what they would play, what they would hold. The market in general. These are big articles usually about a 6 sentence paragraph.

The users are complaining about two things.

One: that the Meta refresh is taking to long.
ie: They are missing updates because we are refreshing every 2 minutes or so.

Two: That the page is refreshing but there is no new content so they are losing there place on the page for no reason.

problem one wouldn't be a problem if we didn't have the second issue.

Is this solution still going to work? Can you give me a sample of outputing the data from an asp script
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
onebadboyAuthor Commented:
Bobbit31,

How do I have the asp page output it's result?
0
 
bobbit31Commented:
> How do I have the asp page output it's result?

whatever get's output to the browser will get output to your applet instead (w/ the above code)

ie:

<HTML>
<%
    Response.write("this will be read from applet")
%>
</HTML>

the applet will recieve:

<HTML>
this will be read from applet
</HTML>

so instead of outputting as html just output your information as unformatted text and parse out whatever information you need in the applet.
0
 
objectsCommented:
Just reload the page.

if (database has changed)
{
   applet.getAppletContext().showDocument(pageURL);
}
0
 
onebadboyAuthor Commented:
bobit31 & objects

The browser window is not reloading. Is it a problem with Security restrictions? (Also everything works fine when I test this with my local machine but when I transfer it to the server it doesn't work) Here's the code that I have for the Java Applet:

/*
 * GetChanges.java
 *
 * Created on June 17, 2002, 11:24 PM
 */

/**
 *
 * @author  Administrator
 * @version
 */

import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.InetAddress;
import java.io.*;
import java.applet.*;


public class GetChanges extends java.applet.Applet implements Runnable  {

    /** Initialization method that will be called after the applet is loaded
     *  into the browser.
     */
    private Thread t;
   

    public void init () {
         
         
         t = new Thread(this);    
         t.setPriority(Thread.MIN_PRIORITY); // be nice
         t.start();

    }
    public void destroy() {
       
         // stop the thread
         t = null;
    }

    // this is the routine that will query your asp page and disp results
    public void run() {
         String myIndex = this.getParameter("index");
         Thread thisThread = Thread.currentThread();
         
         while (t == thisThread) {
              BufferedReader in = null;
             
              try {
                   
                   // assuming the asp page the checks the db for changes
                   // is in the same folder as the page that holds the applet
                   URL myURL = new URL (getDocumentBase(), "CheckDB.asp?Index=" + myIndex);
                   URLConnection con = myURL.openConnection();
   
                   in = new BufferedReader(new InputStreamReader(con.getInputStream()));
   
                   String s="";
                   String response="";
                             
               // read in result
               while ((s = in.readLine()) != null) {
                   response += s;
               }
               
               if(myIndex.compareTo(response) != 0){
                   
                   URL newURL = new URL(getDocumentBase(),"mm.asp?index=" + response);
                   this.getAppletContext().showDocument(newURL);
                                     
               }
                // response holds the output from CheckDB.asp
                                 
                 // sleep for 10 seconds
               t.sleep(10000);                
              } catch (IOException ioe) {
                   // error handling
              } catch (InterruptedException ie) {
                   // error handling    
              } finally {
                   // close stream
                   try {
                        if (in != null) {     in.close(); }
                   } catch (IOException ie) {
                        // more error handling
                   }
               }
         }

    }
   
}

On the CheckDB.asp page I'm just Querying a Database and returning a number. I know it's returning something because I can go to the URL and it prints the number on the screen. What am I doing wrong or is this just security restrictions. I tried going into IE and letting unsigned applets run everything still it doesn't work?
0
 
onebadboyAuthor Commented:
Thank you for all of your help
0
 
bobbit31Commented:
the applet has to be on the same domain as your asp page.

other than that, you shouldn't have to change any security policies.
0
 
objectsCommented:
Didn't my suggestion also help?
0
All Courses

From novice to tech pro — start learning today.