首页后端开发其他后端知识jsp实现cookie的使用

jsp实现cookie的使用

时间2024-02-07 22:16:02发布访客分类其他后端知识浏览350
导读:收集整理的这篇文章主要介绍了jsp实现cookie的使用,觉得挺不错的,现在分享给大家,也给大家做个参考。 package c...
收集整理的这篇文章主要介绍了jsp实现cookie的使用,觉得挺不错的,现在分享给大家,也给大家做个参考。
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;  i3;  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> ");  
}  
}  
您可能感兴趣的文章:
  • 关于jsp中cookie丢失问题(详解)
  • jsp使用cookie存储中文示例分享
  • 用JSP操作Cookie
  • JSP实现浏览器关闭cookies情况下的会话管理
  • jsp源码实例5(cookie)
  • JSP的Cookie在登录中的使用

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: jsp实现cookie的使用
本文地址: https://pptw.com/jishu/604628.html
JSP计数器的制作 jsp搜索引擎

游客 回复需填写必要信息