JavaWeb-Cookie和Session
2021/8/17 17:06:24
本文主要是介绍JavaWeb-Cookie和Session,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
cookie:存放在客户端
session:存放在服务端
cookie
package com.th0r.servlet; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Date; public class Demo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=utf-8"); if (req.getCookies() != null){ resp.getWriter().print("你上次访问的时间是:"); for (Cookie cookie : req.getCookies()) { if (cookie.getName().equals("lastLogin")){ long l = Long.parseLong(cookie.getValue()); Date date = new Date(l); resp.getWriter().print(date.toString()); } } } else { resp.getWriter().println("这是你第一次访问"); } //设置cookie Cookie cookie = new Cookie("lastLogin", System.currentTimeMillis() + ""); //设置cookie有效期 cookie.setMaxAge(2*60*60); resp.addCookie(cookie); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
第一次访问
第二次访问
- 一个cookie只能保存一个信息
- 一个web站点可以发送多个cookie,最多存放20个
- cookie的大小限制为4kb
- 浏览器的cookie上限为300个
删除cookie的方法
- 不设置有效期,关闭浏览器,自动失效
- 设置有效期为0
Session
- 服务器会给每一个用户(浏览器)创建一个Seesion对象
- 一个Seesion独占一个浏览器,只要浏览器没有关闭,这个Session就存在
- 用户登录之后,整个网站它都可以访向
创建Session
package com.th0r.servlet; import com.th0r.pro.Person; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class Demo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=utf-8"); HttpSession session = req.getSession(); session.setAttribute("name", new Person("Bob", 18)); String sessionId = session.getId(); if (session.isNew()){ resp.getWriter().write("Session 创建成功" + sessionId); } else { resp.getWriter().write("Session 已存在" + sessionId); Person person = (Person)session.getAttribute("name"); resp.getWriter().print("\n"+person); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
销毁session
package com.th0r.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class Demo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=utf-8"); HttpSession session = req.getSession(); if (session.getAttribute("name") != null){ session.removeAttribute("name"); session.invalidate(); resp.getWriter().println("注销成功!"); }else { resp.getWriter().println("未登录!"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
设置时间
web.xml
<session-config> <session-timeout>15</session-timeout> </session-config>
这篇关于JavaWeb-Cookie和Session的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南