| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > 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 current7mxLinux联盟
* session (regardless of how long that session lasts)7mxLinux联盟
* and three that persist for an hour (regardless of7mxLinux联盟
* whether the browser is restarted).7mxLinux联盟
* <P>7mxLinux联盟
* Taken from Core Servlets and JavaServer Pages7mxLinux联盟
* from Prentice Hall and Sun Microsystems Press,7mxLinux联盟
* http://www.coreservlets.com/.7mxLinux联盟
* &copy; 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 cookie7mxLinux联盟
// 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 whether7mxLinux联盟
// 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.println7mxLinux联盟
(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联盟
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门