Hi,
could anybody pls help me out to resolve servlet refresh problem.
My servlet page should be refreshed whenever any events happened.
Here with I posted my code.
If the var is true, I want to refresh my page from run() method.
How to do it ?
Thnanks in advance
------------------------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class MainServlet extends HttpServlet implements Runnable {
public static boolean staticVar = false;
static int var = 11;
HttpServletResponse resp;
Thread thread;
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
MyThread thread = new MyThread();
thread.start();
start();
}
//Process the HTTP Post request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
resp = response;
System.out.println("MainServlet: doGet()");
response.setContentType("text/html");
PrintWriter out = new PrintWriter (response.getOutputStream());
out.println("<html>");
out.println("<head><title>MainServlet</title></head>");
out.println("<body>");
out.println("First Page"+var++);
out.println("</body></html>");
out.close();
} //doGet()
public void start() {
System.out.println("MainServlet: 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("MainServlet: run");
while ( true ) {
try {
if ( staticVar ) {
System.out.println("MainServlet: var is true. so refresh.");
// resp.setHeader("REFRESH", "5");
resp.setContentType("text/html");
PrintWriter out = new PrintWriter (resp.getOutputStream());
out.println("<html>");
out.println("<head><title>New Title</title></head>");
out.println("<body>");
out.println("First Page..........."+var++);
out.println("</body></html>");
out.close();
break;
} // if
try { Thread.sleep(1000); }catch(InterruptedException io) { }
} catch(IOException ioe) { }
} // while
} // run()
} // MainServlet
---------------------------------------------------------------------------
class MyThread extends Thread {
//Construct the application
public MyThread() {
System.out.println("MyThread: constructor");
} // constructor
public void run() {
System.out.println("MyThread: run()");
while ( true ) {
try { Thread.sleep(2000); } catch(InterruptedException ie) { }
if ( MainServlet.staticVar == false ) {
MainServlet.staticVar = true;
System.out.println("MyThread: var set to true");
} // if
} // while
} // run()
} // MyThread
--------------------------------------------------------------------------
___________________________________________________________________________
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