您的位置:首页网页设计JSP实例 → jsp源码实例5(cookie)

jsp源码实例5(cookie)

时间:2004/11/7 3:38:00来源:本站整理作者:蓝点我要评论(0)

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).

*



* 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) +

"\n" +

"

" + title + "

\n" +

"There are six cookies associated with this page.\n" +

"To see them, visit the\n" +

"\n" +

"ShowCookies servlet
.\n" +

"

\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" +

"ShowCookies servlet to verify that\n" +

"the three long-lived ones persist across sessions.\n" +

"");

}

}


相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 没有查询到任何记录。 学习java必学的几门技术jspSmartUpload上传下载全攻略Tomcat5.x中的虚拟主机配置方法利用iText在JSP中生成PDF报表

人气排行 告诉大家JSP连接数据库程序代码JSP单页面网站文件管理器jsp留言板源代码一: 给jsp初学者.在jsp中用bean和servlet联合实现用户注册、使用JSP + JAVABEAN + XML 开发的一个例子jsp在线考试系统-jsp文件 jsp计数器代码JSP/JAVABEAN+TOMCAT4.0.5+MYSQL组合建站总