Hi,
 I have a servlet which draws HTML page from doGet().
I want to refresh/redraw my servlet outside of doGet()/doPost().
(I have a thread in my servlet which check the variable is set to
true or not. If true I want to redraw/refresh my screen.)
so I tried thru' 'server push' method. If i do from doGet() it's working.
If I try to refresh from run() it's not working.
so how to refresh the page whenever I need ?

Tnanx in advance

my code
-------

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MultipartResponse implements Runnable {

  HttpServletResponse res;
  ServletOutputStream out;
  boolean endedLastResponse = true;
  Thread thread = null;
  public MultipartResponse(HttpServletResponse response) throws IOException {
     System.out.println("MultiPartResponse: constructor()");
    res = response;
    out = res.getOutputStream();
    res.setContentType("multipart/x-mixed-replace;boundary=End");
    out.println();
  }
  public void startResponse(String contentType) throws IOException {
    if (!endedLastResponse) {
      endResponse();
    }
    // Start the next one
    out.println("Content-type: " + contentType);
    out.println();
    endedLastResponse = false;
  }
  public void endResponse() throws IOException {
    out.println();
    out.flush();
    endedLastResponse = true;
  }
  public void finish() throws IOException {
    out.println("--End--");
    out.flush();
  }

    public void start() {
     System.out.println("MultiPartResponse: start()");
    if ( thread == null ) {
      thread = new Thread(this);
      thread.start();
    } // if
  } // start()

  public void stop() {
    if ( thread != null ) {
      thread.stop();
      thread = null;
    } // if
  } // stop()

  public void run() {
     System.out.println("MultiPartResponse: run()");
    while ( true ) {
     MultiPartEx.UserSleep(10000);
     MultiPartEx.staticVar = true;
     System.out.println("MultiPartResponse: var set to true.");
     break;
    } // while

  } // run()

} // End Of MultipartResponse

-----------------------------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class MultiPartEx extends HttpServlet implements Runnable {

    public static boolean staticVar = false;
    Thread thread = null;
    MultipartResponse multi = null;

    public void doGet(HttpServletRequest req, HttpServletResponse res)
                        throws ServletException, IOException {

    System.out.println("MultiPartEx: doGet");
       res.setContentType("text/plain");
       PrintWriter out = new PrintWriter(res.getOutputStream());
       multi = new MultipartResponse(res);
       multi.start();
       multi.startResponse("text/plain");
       multi.out.println("I am first. Wait for 5 sec");
       multi.endResponse();
       start();
/*
       UserSleep(5000);

       multi.startResponse("text/html");
       multi.out.println("I am 2nd.  Refreshing over. Bye...");
       multi.endResponse();

       multi.finish();
*/
    } // doGet()


  public void start() {
    System.out.println("MultiPartEx: start()");
    if ( thread == null ) {
      thread = new Thread(this);
      thread.start();
    } // if
  } // start()

  public void stop() {
    if ( thread != null ) {
      thread.stop();
      thread = null;
    } // if

  } // stop()

  public void run() {
   System.out.println("MultiPartEx: run");
    while ( true ) {
    try {
      if ( staticVar ) {
       System.out.println("MultiPartEx: var is true. refresh.");
       multi.startResponse("text/plain");
       multi.out.println("I am 2nd.  Refreshing over. Bye..");
       multi.endResponse();
       multi.finish();
       staticVar = false;
       System.out.println("MultiPartEx: refreshed.");
       return;
      } // if
      UserSleep(2000);
      } catch(IOException ioe) { }
    } // while
  } // run()

    public static void UserSleep(int sleepTime)
   {
    try {
     Thread.sleep(sleepTime);
    }catch(Exception e) { }
  }
} // End Of Class MultipartEx

------------------------------------------------------------------------

I run it by, http://mymachine/servlet/MultiPartEx
and i am getting "I am first. Wait for 5 sec" msg only,
I am not getting the 2nd one. How to do it ?
Pls help me.
Thanx in advance.

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to