to the main page about the tutorial  INTERACTIVE WEB SERVICES WITH JAVA back up next

Sessions

Session state is maintained in an HttpSession object: - this resembles the management of shared state.

A typical example:
...
HttpSession session = request.getSession(true);
ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingcart");
if (cart==null) {
    cart = new ShoppingCart();
    session.setAttribute("shoppingcart", cart);
}
addItemToCart(cart);
...

Under the hood: uses cookies or URL rewriting

(Since URL rewriting may be used, URLs to our own Servlets should always be passed through response.encodeURL(String))

Cookies can also be controlled manually...

This is a rather low-level approach - the session flow is not explicit in the code!

back COPYRIGHT © 2002-2003 ANDERS MØLLER & MICHAEL I. SCHWARTZBACH next