Package coreservlets;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
/** Sets six Cookies:three that apply
* Session (regardless of how long, session lasts)
* and three that persist for a hour (regardless of
* Whether the browser is restarted).
* <P>
* Taken from Core Servlets and JavaServer Pages
* FROM Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
*©2000 Marty Hall; May is 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 browsing session.
Cookie 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 a for 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" +
"
"There are six cookies associated with this page.\n" +
"To 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 across sessions.\n" +
"</BODY></HTML>");
}
}