Nicolas Frankel is an IT consultant with 10 years experience in Java / JEE environments. He likes his job so much he writes technical articles on his blog and reviews technical books in his spare time. He also tries to find other geeks like him in universities, as a part-time lecturer. Nicolas is a DZone MVB and is not an employee of DZone and has posted 221 posts at DZone. You can read more from them at their website. View Full User Profile

Obtaining Request Data in Vaadin 7

03.12.2012
| 6620 views |
  • submit to reddit

I confess, Vaadin 6 was good, but Vaadin 7 is even better. This article will detail most nastiness that is managed by this new version so that we only have to code a few lines to achieve our goals. One of such goal is the search of information about the request and the client.

In Vaadin 6, it was possible, but cumbersome for we had to force our application to implement the HttpServletRequestListener interface. The interface let us access the servlet request (as well as the servlet response), and then we were on our own.

In Vaadin 7, the Vaadin team identified recurring needs to some piece of data and provided a Vaadin API to access them easily; for example:

  1. The user agent type and its version (major and minor)
  2. The user's locale to initialize resources bundles
  3. The screen size, as well as the underlying viewpoint size to adapt the layour to the available space
  4. The URL fragment (#) to restore data

Vaadin 7 Root's init() method takes a WrapperRequest as a parameter, that is the entry point into these informations. Behold how we could address the above needs:

public class Vaadin7Root extends Root {
 
    @Override
    public void init(WrappedRequest wrapped) {
 
        BrowserDetails details = wrapped.getBrowserDetails();
 
        WebBrowser browser = details.getWebBrowser();
 
        // Need #1
        browser.getBrowserApplication();    // User agent
        browser.getBrowserMajorVersion();   // Major version
        abrowser.getBrowserMinorVersion();  // Minor version
 
        // Need #2
        browser.getLocale();                // User locale
         
        // Need #3
        browser.getClientHeight();          // Browser height
        browser.getClientWidth();           // Browser width
        browser.getScreenHeight();          // Viewpoint height
        browser.getScreenWidth();           // Viewpoint width
 
        // Need #4
        details.getUriFragment());          // URI fragment
    }

 No more dirty plumbing into the ServletRequest, welcome to a world of easy reading!

The ready-to-use WAR file is available here (sources included).

Note: There's a large difference in behavior between version 6 and 7 of Vaadin. In Vaadin 7, refreshing the browser will cause the init() method to be called again. 

Published at DZone with permission of Nicolas Frankel, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)