Accessing web page and browser information

Vaadin 7 includes a new Page class offering access to various client-side information and events concerning the web page and browser window in which the Vaadin UI resides. The Page instance corresponding to a given UI is accessed via the getPage() method of the UI or using a static method Page.getCurrent().

You can access the browser window size and add size change listeners:

Java

  1. Page page = someUI.getPage();
  2. page.addBrowserWindowResizeListener(new BrowserWindowResizeListener() {
  3. public void browserWindowResized(BrowserWindowResizeEvent event) {
  4. Notification.show("Window width=" + event.getWidth() + ", height=" + event.getHeight());
  5. }
  6. });

You can access the optional fragment part of the location URI and add fragment change listeners:

Java

  1. page.setUriFragment(page.getUriFragment() + "foo");
  2. page.addUriFragmentChangedListener(new UriFragmentChangedListener() {
  3. public void uriFragmentChanged(UriFragmentChangedEvent event) {
  4. Notification.show("Fragment=" + event.getUriFragment());
  5. }
  6. });

You can access client browser details:

Java

  1. Notification.show("IP" + browser.getAddress() +
  2. "User-Agent:" + browser.getBrowserApplication() +
  3. "Linux: " + browser.isLinux());

Live Demo

Note: If you are using a reverse proxy, you must get the value X-Forwarded-For from request headers. You cannot get a browser name, but you can check which browser are using.