当前位置: 首页 > 图文教程 > 网络编程 > JSP > jsp源码实例5(cookie)

JSP
我认为JSP有问题(下)
JSP显示中文问题的解决方案
JSP技术简介
JSP开发导引
JSP开发入门
JSP多种web应用服务器导致JSP源码泄漏漏洞
JSP实现浏览器关闭cookies情况下的会话管理
tomcat 3.1在RedHat下的安装
初学jsp心得
如何防止IE缓存jsp文件
利用Java实现zip压缩/解压缩
一个用JSP做的日历
无边框窗口代码详解
实例讲解JSP Model2体系结构(上)
实例讲解JSP Model2体系结构(中)
实例讲解JSP Model2体系结构(下)
JSP模板应用指南(下)
JSP简明教程:令人兴奋的脚本编程
JSP简明教程:对比与总结
jsp基础学习资料

JSP 中的 jsp源码实例5(cookie)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 40 ::
收藏到网摘: n/a

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