源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 393|回复: 12

[JSP编程] jsp实现cookie的使用

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2006-10-13 00:00:00 | 显示全部楼层 |阅读模式

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


回复

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-10-10 09:18:09 | 显示全部楼层
啪啪啪生怕PSP怕
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

221

积分

中级会员

Rank: 3Rank: 3

积分
221
发表于 2024-3-12 20:35:27 | 显示全部楼层
源码源码源码源码源码源码源码源码源码源码源码源码源码
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

146

积分

注册会员

Rank: 2

积分
146
发表于 2024-3-18 14:09:07 | 显示全部楼层
。。。。。。。。。。。。。。。
回复 支持 反对

使用道具 举报

11

主题

1万

回帖

300

积分

中级会员

Rank: 3Rank: 3

积分
300
发表于 2024-5-22 13:43:42 | 显示全部楼层
2222222222222222
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

55

积分

注册会员

Rank: 2

积分
55
发表于 2024-5-26 12:56:03 | 显示全部楼层
啦啦啦啦啦啦啦啦!
回复 支持 反对

使用道具 举报

14

主题

1万

回帖

75

积分

注册会员

Rank: 2

积分
75
发表于 2024-6-9 23:25:31 | 显示全部楼层
好东西可以可以可以可以
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

87

积分

注册会员

Rank: 2

积分
87
发表于 2024-6-21 03:15:34 | 显示全部楼层
天天源码社区。。。。
回复 支持 反对

使用道具 举报

6

主题

1万

回帖

425

积分

中级会员

Rank: 3Rank: 3

积分
425
发表于 2024-7-4 04:01:21 | 显示全部楼层
谢谢楼主分享
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

100

积分

注册会员

Rank: 2

积分
100
发表于 2024-7-19 03:42:08 | 显示全部楼层
逛逛看看瞧瞧
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-9-20 21:23 , Processed in 0.087737 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表