?
Solved

Java Applet Browser Refresh

Posted on 2002-06-17
10
Medium Priority
?
1,252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

770 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