Package coreservlets;
Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
/** Sets six cookies: Three that apply only to the current
* Session (regardless of how long that session lasts)
* And three that persist for an hour (regardless
* Whether the browser is restarted ).
* <P>
* Taken from core servlets and assumerver pages
* From Prentice Hall and Sun Microsystems press,
* Http://www.coreservlets.com /.
* 2000 Marty Hall; may be freely used or adapted.
*/
Public class setcookies extends httpservlet {
Public void doget (httpservletrequest request,
Httpservletresponse response)
Throws servletexception, ioexception {
For (INT I = 0; I <3; I ++ ){
// Default maxage is-1, indicating cookie
// Applies only to current browsing session.
Cookie = new cookie ("session-cookie-" + I,
"Cookie-value-s" + I );
Response. addcookie (cookie );
Cookie = new cookie ("persistent-cookie-" + I,
"Cookie-value-P" + I );
// Cookie is valid for an hour, regardless of whether
// User quits browser, reboots computer, or whatever.
Cookie. setmaxage (3600 );
Response. addcookie (cookie );
}
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
String title = "setting cookies ";
Out. println
(Servletutilities. headwithtitle (title) +
"<Body bgcolor = \" # fdf5e6 \ "> \ n" +
"<H1 align = \" center \ ">" + title + ""There are six cookies associated with this page. \ n" +
"To see them, visit the \ n" +
"<A href = \"/servlet/coreservlets. showcookies \ "> \ n" +
"<Code> showcookies </code> servlet </a>. \ n" +
"<P> \ n" +
"Three of the cookies are associated only with the \ n" +
"Current session, while three are persistent. \ n" +
"Quit the browser, restart, and return to the \ n" +
"<Code> showcookies </code> servlet to verify that \ n" +
"The three long-lived ones persist extends SS sessions. \ n" +
"</Body> }
}