jsp实现cookie的使用
程序员文章站
2024-03-31 11:38:22
package coreservlets; import java.io.*;...
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 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 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 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 + "</h1>\n" +
"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 across sessions.\n" +
"</body></html>");
}
}
推荐阅读
-
CI框架实现cookie登陆的方法详解_php实例
-
浏览器打开页面实现文件下载的程序代码(php/jsp/java)
-
yii2使用ajax返回json的实现方法,yii2ajax返回json_PHP教程
-
yii2.0使用Plupload实现带缩放功能的多图上传_php实例
-
php同时使用session和cookie来保存用户登录信息的实现代码,sessioncookie_PHP教程
-
怎么使用PHPMailer实现邮件的发送??,phpmailer邮件
-
请问关于php中使用ajax实现菜单联动查询的思路和方法
-
yii2.0使用Plupload实现带缩放功能的多图上传_php实例
-
探讨:如何使用PHP实现计算两个日期间隔的年、月、周、日数
-
php中cookie实现二级域名可访问操作的方法