jsp实现cookie的使用
程序员文章站
2023-12-15 16:49:46
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>");
}
}
推荐阅读
-
用JSP实现的一个日历程序
-
jsp实现cookie的使用
-
Android使用Sensor感应器实现线程中刷新UI创建android测力计的功能
-
Android编程使用GestureDetector实现简单手势监听与处理的方法
-
Android中使用Kotlin实现一个简单的登录界面
-
android中使用Html渲染的方式实现必填项前面的*号示例
-
Android中使用Handler及Countdowntimer实现包含倒计时的闪屏页面
-
C#中通过使用Connection类来实现打开/关闭数据库的代码实例
-
C#使用FileSystemWatcher控件实现的文件监控功能示例
-
Android编程使用自定义shape实现shadow阴影效果的方法