13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

414Chapter 18Servlets: Java Pressed into Servicec<strong>on</strong>necti<strong>on</strong> is g<strong>on</strong>e. 2 About the best <strong>on</strong>e can hope for is that you’ll use what,in our postal analogy, would be like a supplied reply envelope. This allows theservlet engine of the Web server to track requests from the same user and providea sessi<strong>on</strong> capability across requests. It will use your browsers cookie mechanismto store this sessi<strong>on</strong>’s ID used to track your sessi<strong>on</strong>. If you d<strong>on</strong>’t havesessi<strong>on</strong>s <strong>on</strong>, it will need to use URL rewriting, whereby the URLs generatedwill have an added parameter, the sessi<strong>on</strong> ID.Unlike the early days in the life of the Web, nowadays virtually every<strong>on</strong>ehas cookies enabled in their browsers—any<strong>on</strong>e who shops at amaz<strong>on</strong>.com, atleast. This makes sessi<strong>on</strong> tracking so much easier for the servlet developer. TheWeb server handles all that automatically, and you <strong>on</strong>ly need to make a fewcalls to the sessi<strong>on</strong>-related methods of the HttpRequest.To get a sessi<strong>on</strong> for a user, ask for <strong>on</strong>e from the HttpRequest:HttpSessi<strong>on</strong> sessi<strong>on</strong> = request.getSessi<strong>on</strong>(true);The boolean parameter says whether (true) or not to create a sessi<strong>on</strong> if<strong>on</strong>e does not yet exist for this user. Once you have a sessi<strong>on</strong>, you can storeobjects associated with that sessi<strong>on</strong>:sessi<strong>on</strong>.setAttribute("cart", shopCart);where shopCart is any serializable Object and "cart" could be any Stringthat you want to use to later identify and retrieve this object, for example:Basket myCart = (Basket) sessi<strong>on</strong>.getAttribute("cart");Notice that we need to explicitly cast the object type returned bygetAttribute(), because it returns a generic Object.18.6.1 CookiesFor any informati<strong>on</strong> that you want to save for l<strong>on</strong>ger than the durati<strong>on</strong> of asessi<strong>on</strong>, you may want to investigate cookies—little bits of data (4K max; typically<strong>on</strong>ly a few bytes) sent to the browser for it to store and send back at a latertime. You make a cookie thus:2. You can go to another page, just be staring at the page for a l<strong>on</strong>g l<strong>on</strong>g time, or you mighthave shut down your browser completely—and the server-side servlet will never know.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!