Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
192
Chapter 8 Handling Cookies
Listing 8.4 LongLivedCookie.java 
package coreservlets;
import javax.servlet.http.*;
/** Cookie that persists 1 year. Default Cookie doesn t
 *  persist past current session.
 */
public class LongLivedCookie extends Cookie {
  public static final int SECONDS_PER_YEAR = 60*60*24*365;
  
  public LongLivedCookie(String name, String value) {
    super(name, value);
    setMaxAge(SECONDS_PER_YEAR);
  }
}
reads the user choices from the HTML front end and forwards them to the
appropriate search engine. In addition, the 
CustomizedSearchEngines
servlet returns to the client cookies that store the search values. Then, when
the user comes back to the front end servlet at a later time (even after quit 
ting the browser and restarting), the front end page is initialized with the val 
ues from the previous search. 
To accomplish this customization, the front end is dynamically generated
instead of coming from a static HTML file (see Listing 8.6 for the source
code and Figure 8 4 for the result). The front end servlet reads the cookie
values and uses them for the initial values of the HTML form fields. Note
that it would not have been possible for the front end to return the cookies
directly to the client. That's because the search selections aren't known until
the user interactively fills in the form and submits it, which cannot occur until
after the servlet that generated the front end has finished executing.
This example uses the 
LongLivedCookie
 class, shown in the previous sec 
tion, for creating a 
Cookie
 that automatically has a long term expiration date,
instructing the browser to use it beyond the current session. 
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved