Behind the event-driven processing model of Vaadin lies the Java Servlet API, which is based on processing HTTP requests. These requests are normally hidden from Vaadin applications, but can be caught using the HttpServletRequestListener interface. You must implement the interface in your application class. The two methods defined in the interface, onRequestStart() and onRequestEnd(), allow processing the request before and after other processing.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;
import com.vaadin.ui.*;

public class HttpServletRequestApplication extends Application
       implements HttpServletRequestListener {

    @Override
    public void init() {
        System.out.println("  Application.init() called.");
        
        Window main = new Window("URI Fragment Example");
        setMainWindow(main);
        setTheme("book-examples");
                
        // Does nothing but causes a request
        Button button = new Button ("Make a request");
        main.addComponent(button);
    }

    public void onRequestStart(HttpServletRequest request,
                               HttpServletResponse response) {
        System.out.println("[Start of request");
        System.out.println(" Query string: " +
                           request.getQueryString());
        System.out.println(" Path: " +
                           request.getPathInfo());
    }

    public void onRequestEnd(HttpServletRequest request,
                             HttpServletResponse response) {
        System.out.println(" End of request]");
    }
}

The onRequestStart() is called for the first time when the application class is loaded but the init() is not yet called. This can be seen in the output of the above code example:

[Start of request
 Query string: null
 Path: null
  Application.init() called.
 End of request]
[Start of request
 Query string: repaintAll=1&sh=1050&sw=1680&cw=500&ch=300&vw=500
 Path: /UIDL/
 End of request]
[Start of request
 Query string: windowName=1071684214
 Path: /UIDL/
 End of request]

The first call is a regular HTML page load, so the URL path is simply the application path. The subsequent calls are AJAX calls made using the UIDL protocol, so the request path includes the /UIDL/ part. This is important to know when using cookies, as explained later.

Setting and reading cookies is one of the typical uses of HttpServletRequestListener. The application gets the HttpServletRequest object containing the cookies in the onRequestStart() method.

You normally set a cookie in an event listener. As the request object is a transient object that exists only for the duration of the request, it is not accessible from the Application object. The only way to access it is to store it in onRequestStart(), as done in the following example.

public class CookieExampleApplication extends Application
       implements HttpServletRequestListener {
    HttpServletResponse response;

    public void onRequestStart(HttpServletRequest request,
                               HttpServletResponse response) {
        // Store the reference to the response object for
        // using it in event listeners
        this.response = response;
        ...
   }
   ...

We can then use the reference to set or delete cookies in event listeners. Notice that the cookie path property is automatically set to the application path (such as /book-examples/cookies) on the first request, but contains the UIDL subpath on subsequent calls (such as /book-examples/cookies/UIDL). As the cookies are matched against this path, you may need to set the path explicitly with setPath().

newuser = new TextField ("Give a user name");
login = new Button("Login");
login.addListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        Object value = newuser.getValue(); 
        if (value != null &&
            ! "".equals((String)value)) {
            username = (String) value;

            Cookie cookie = new Cookie("username",
                                       username);
            // Use a fixed path
            cookie.setPath("/book-examples");
            cookie.setMaxAge(3600); // One hour
            response.addCookie(cookie);
            System.out.println("Set cookie.");

            newuser.setEnabled(false);
            login.setEnabled(false);
            restart.setEnabled(true);
            logout.setEnabled(true);
        }
    }
});
loginrow.addComponent(newuser);
loginrow.addComponent(login);

Removing cookie can be set in similar way by setting the maxAge property to zero.

// Delete the cookie
Cookie cookie = new Cookie("username", username);
cookie.setPath("/book-examples");
cookie.setMaxAge(0); // Delete
response.addCookie(cookie);