A servlet that must generate a new page to a browser at any time without browser request for it
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, April 19, 2002 05:49 AM

counter desde el servidor I need to make a servlet that shows at the client browser a page with the value of a variable in the servlet. This variable is increasing each three seconds. The first time that the client browser calls to the servlet, a page is showed and is colled (at the servlet) the "show_counter" method, that is inside a bucle and it should refresh the page on the client browser each three seconds to show the new value of the counter. This program is not working. Can you say me if must use multithread or wath is the problem? Thanks This is the source code of the program: import java.io.   More>>
counter desde el servidor

I need to make a servlet that shows at the client browser a page with the value of a variable

in the servlet. This variable is increasing each three seconds. The first time that the client

browser calls to the servlet, a page is showed and is colled (at the servlet) the

"show_counter" method, that is inside a bucle and it should refresh the page on the client

browser each three seconds to show the new value of the counter.

This program is not working. Can you say me if must use multithread or wath is the

problem?



Thanks



This is the source code of the program:




import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;



public class Master extends HttpServlet

{

int started = 0;

int counter;

long last_time = 0;

long current_time = 0;





public void init (ServletConfig config) throws

ServletException

{

super.init (config);

counter = 0;

last_time = System.currentTimeMillis();

}





protected void doGet (HttpServletRequest req,

HttpServletResponse res)

throws ServletException, IOException

{

if (started == 0)

{

started = 1;



StringBuffer buffer = new StringBuffer();



buffer.append (" ");

buffer.append (" ");

buffer.append (" Pantalla regenerada. Paso ");

buffer.append (Integer.toString (counter));

buffer.append (" ");



res.setContentType ("text/html");

res.setContentLength (buffer.length());

res.setHeader ("pragma", "no-cache");

res.getOutputStream().print (buffer.toString());



show_counter(req, res);

}

}





protected void show_counter (HttpServletRequest req,

HttpServletResponse res)

throws IOException

{

while (counter < 20)

{

current_time = System.currentTimeMillis();

if ((current_time - last_time) >= 3000)

{

StringBuffer buffer = new StringBuffer();



last_time = current_time;

counter++;



buffer.append (" ");

buffer.append (" counter desde el servidor ");

buffer.append (" Pantalla regenerada. Paso ");

buffer.append (Integer.toString (counter));

buffer.append (" ");



res.setContentType ("text/html");

res.setContentLength (buffer.length());

res.setHeader ("pragma", "no-cache");

res.getOutputStream().print (buffer.toString());

}

}

}





}

   <<Less

Re: A servlet that must generate a new page to a browser at any time without browser request for it

Posted By:   Zac_Jacobson  
Posted On:   Tuesday, April 23, 2002 11:06 AM

Browsers are not, by their nature, good push clients.

Here's an idea for you: Write an applet, or even an app. The app(let) registers itself with your serv which then has a list of clients that are listening to information. When something happens, it just sends a message to each of the registered listners.

Stuff to consider:

  • This communication will most likely be outside of the web server, if you need to have your data protected, you'll have to do it yourself.
  • If you have an applet, your applet should try to un-register itself when the browser stops it. The serv should be able to handle itself if it can't reach some of its listeners.

Here's another idea that may solve your problems:

Do what you're doing now, with the 3-second refresh from the browser. But don't bother to keep your browser pointed at the servlet all the time, have it send an email (lots of pagers accept email) when something is changing, and then you only have to tax your network with something is going on.

Good luck!

Zac

Re: A servlet that must generate a new page to a browser at any time without browser request for it

Posted By:   pradeep_kanwar  
Posted On:   Friday, April 19, 2002 03:57 PM

what you need is ask the client to call you after three seconds ( isn't that too little a time to call a refresh !!!! ) so you should not be bothering the server side code for it.


javascript provides a settimeout("dostuffhere like calling your page url", repeatseconds); method which is cool when refreshing data

or

you can use the meta refresh mentioned earllier in the page.
personally i like settimeout() because i face suspicious :| problems with meta refresh


look for 'refresh' on jguru forums and u might have see your answer already answered :)

Re: A servlet that must generate a new page to a browser at any time without browser request for it

Posted By:   Christopher_Schultz  
Posted On:   Friday, April 19, 2002 06:55 AM

First, your content-length is all wrong: you're setting it before you know how long the whole HTML page will be.



I'd suggest not setting the content-length at all.



Secondly, how are you trying to refresh the page every 3 seconds? You are just waiting 3*20 seconds the first time the user hits the page and spitting out a new piece of HTML each time.



I suggest you do some more reading on HTTP and the servlet process.



What you really want to do is set a META-HTTP-EQUIV="Refresh" with a timeout value of 3, and have the client call the servlet every three seconds.



-chris
About | Sitemap | Contact