GWT, Java

Auto refresh in GWT

Say you have a Google Web Toolkit (GWT) application, and you want a page to auto refresh after a set time interval. It is very easy to achieve using GWT Timer.


	private Timer t;
	
	public void autoRefresh() {
	
		t = new Timer() {
			public void run() {
				Window.Location.reload();
			}
		};
		t.schedule(10000); // Auto refresh every 10 secs
	}
	
	public void stopAutoRefresh() {
		
		t.cancel();
	}

GWT

How to determine whether a GWT app is running in hosted mode or web mode?

There are instances when you have to determine whether a GWT application is running in hosted mode or web mode. This method will prove to be helpful in determining if a GWT application is running in hosted mode or not.

	public boolean isHostedMode() {
		return GWT.isClient() && !GWT.isScript();
	}

GWT.isClient():
Returns true when running inside the normal GWT environment, either in hosted mode or web mode. Returns false if this code is running in a plain JVM.

GWT.isScript():
Determines whether or not the running program is script or bytecode. The program is in bytecode while running in hosted mode and is in script while running in web mode. Therefore, the isScript() method returns false in hosted mode and returns true in web mode.