Solved

Java Applet Browser Refresh

Posted on 2002-06-17
10
1,251 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 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
Independent Software Vendors: 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

691 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