登陆论坛
|
论坛注册
|
加入收藏
|
设为首页
|
RSS
首页
Linux频道
软件下载
开发语言
技术论坛
嵌入式频道
|
数据库开发
|
WEB开发
|
软件开发
|
嵌入应用
|
内核研究
|
SHELL
|
输入您的搜索字词
提交搜索表单
您当前的位置:
首页
>
Linux频道
>
Linux开发区
>
WEB开发
jsp源码实例5(cookie)
时间:2007-07-27 11:48:38 来源:
Linux联盟收集整理
作者:
package coreservlets;
7mxLinux联盟
7mxLinux联盟
import java.io.*;
7mxLinux联盟
import javax.servlet.*;
7mxLinux联盟
import javax.servlet.http.*;
7mxLinux联盟
7mxLinux联盟
/** Sets six cookies: three that apply only to the current
7mxLinux联盟
* session (regardless of how long that session lasts)
7mxLinux联盟
* and three that persist for an hour (regardless of
7mxLinux联盟
* whether the browser is restarted).
7mxLinux联盟
* <P>
7mxLinux联盟
* Taken from Core Servlets and JavaServer Pages
7mxLinux联盟
* from Prentice Hall and Sun Microsystems Press,
7mxLinux联盟
* http://www.coreservlets.com/.
7mxLinux联盟
* © 2000 Marty Hall; may be freely used or adapted.
7mxLinux联盟
*/
7mxLinux联盟
7mxLinux联盟
public class SetCookies extends HttpServlet {
7mxLinux联盟
public void doGet(HttpServletRequest request,
7mxLinux联盟
HttpServletResponse response)
7mxLinux联盟
throws ServletException, IOException {
7mxLinux联盟
for(int i=0; i<3; i++) {
7mxLinux联盟
// Default maxAge is -1, indicating cookie
7mxLinux联盟
// applies only to current browsing session.
7mxLinux联盟
Cookie cookie = new Cookie("Session-Cookie-" + i,
7mxLinux联盟
"Cookie-Value-S" + i);
7mxLinux联盟
response.addCookie(cookie);
7mxLinux联盟
cookie = new Cookie("Persistent-Cookie-" + i,
7mxLinux联盟
"Cookie-Value-P" + i);
7mxLinux联盟
// Cookie is valid for an hour, regardless of whether
7mxLinux联盟
// user quits browser, reboots computer, or whatever.
7mxLinux联盟
cookie.setMaxAge(3600);
7mxLinux联盟
response.addCookie(cookie);
7mxLinux联盟
}
7mxLinux联盟
response.setContentType("text/html");
7mxLinux联盟
PrintWriter out = response.getWriter();
7mxLinux联盟
String title = "Setting Cookies";
7mxLinux联盟
out.println
7mxLinux联盟
(ServletUtilities.headWithTitle(title) +
7mxLinux联盟
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
7mxLinux联盟
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
7mxLinux联盟
"There are six cookies associated with this page.\n" +
7mxLinux联盟
"To see them, visit the\n" +
7mxLinux联盟
"<A HREF=\"/servlet/coreservlets.ShowCookies\">\n" +
7mxLinux联盟
"<CODE>ShowCookies</CODE> servlet</A>.\n" +
7mxLinux联盟
"<P>\n" +
7mxLinux联盟
"Three of the cookies are associated only with the\n" +
7mxLinux联盟
"current session, while three are persistent.\n" +
7mxLinux联盟
"Quit the browser, restart, and return to the\n" +
7mxLinux联盟
"<CODE>ShowCookies</CODE> servlet to verify that\n" +
7mxLinux联盟
"the three long-lived ones persist across sessions.\n" +
7mxLinux联盟
"</BODY></HTML>");
7mxLinux联盟
}
7mxLinux联盟
}
7mxLinux联盟
来顶一下
返回首页
发表评论
共有
条评论
用户名:
密码:
验证码:
匿名发表
相关文章
jsp源码实例4(搜索引擎)
jsp源码实例3(获取jsp各种参数)
jsp源码实例2(获取表单参数)
jsp源码实例1(输出)
栏目更新
栏目热门
站内搜索:
Linux频道
下载频道
图库
商品
嵌入式频道
高级搜索
网站首页
|
关于我们
|
服务条款
|
广告服务
|
联系我们
|
网站大全
|
免责声明
|
返回顶部
Copyright © 2007-2008 xxlinux.com, All rights reserved.
Powered by linux联盟
京ICP备05012402号