Use of cookies through JSP

Source: Internet
Author: User


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> }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.