JSP source code Instance 5 (cookie)

Source: Internet
Author: User
Cookie|js



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>");
}
}




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.