jsp源码实例5(cookie)

所属分类: 网络编程 / JSP编程 阅读数: 1772
收藏 0 赞 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).
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* &copy; 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>");
}
}
更多精彩内容其他人还在看

spring动态bean注册示例分享

这篇文章主要介绍了spring动态bean注册示例,需要的朋友可以参考下
收藏 0 赞 0 分享

JSP判断移动设备的正则

天猫php判断移动设备的正则(个人猜测),觉得很好用,于是就决定移植到JSP里面,大家可以参考下
收藏 0 赞 0 分享

jsp文件绝对路径的设置方法

这篇文章主要介绍了jsp文件绝对路径的设置方法,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp与sql语句的混合使用示例

这篇文章主要介绍了jsp与sql语句的混合使用,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp获取action传来的session和session清空以及判断

这篇文章主要介绍了jsp获取action传来的session和session清空以及判断,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp简单自定义标签的forEach遍历及转义字符示例

这篇文章主要介绍了jsp简单自定义标签的forEach遍历及转义字符,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp自定义标签之ifelse与遍历自定义标签示例

这篇文章主要介绍了jsp自定义标签之ifelse与遍历自定义标签,需要的朋友可以参考下
收藏 0 赞 0 分享

JSP加载JS文件不起作用的有效解决方法

jsp导入jquery文件,老是不起作用,原因在于其不能访问/WEB-INF/目录下的文件,下面有个不错的解决方法,大家可以参考下
收藏 0 赞 0 分享

Jsp中如何让图片在div中居中

这篇文章主要介绍了Jsp中如何让图片在div中居中的小技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

使用jsp调用javabean实现超简单网页计算器示例

这篇文章主要介绍了使用jsp和javabean实现超简单网页计算器示例,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多