Solved

Java Applet Browser Refresh

Posted on 2002-06-17
10
1,241 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:onebadboy
  • 5
  • 3
  • 2
10 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 500 total points
ID: 7084329
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
 
LVL 1

Author Comment

by:onebadboy
ID: 7084361
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
 
LVL 1

Author Comment

by:onebadboy
ID: 7084365
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
 
LVL 1

Author Comment

by:onebadboy
ID: 7084434
Bobbit31,

How do I have the asp page output it's result?
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7084472
> 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 92

Expert Comment

by:objects
ID: 7084479
Just reload the page.

if (database has changed)
{
   applet.getAppletContext().showDocument(pageURL);
}
0
 
LVL 1

Author Comment

by:onebadboy
ID: 7090064
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
 
LVL 1

Author Comment

by:onebadboy
ID: 7090217
Thank you for all of your help
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7090244
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
 
LVL 92

Expert Comment

by:objects
ID: 7091220
Didn't my suggestion also help?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now