JFinal Cookie Session

Cookie和Session的区别

1.cookie数据存放在客户的浏览器上,session数据放在服务器上
2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗,如果主要考虑到安全应当使用session
3.session会在一定时间内保存在服务器上。当访问增多,会比较占用你服务器的性能,如果主要考虑到减轻服务器性能方面,应当使用COOKIE
4.单个cookie在客户端的限制是3K,就是说一个站点在客户端存放的COOKIE不能3K。
5.所以:将登陆信息等重要信息存放为SESSION;其他信息如果需要保留,可以放在COOKIE中

Session的运行逻辑在这里插入图片描述

Cookie加密

1.采用md5加密
2.可以设置一个随机字符串#用户信息#时间#这样的字符串经常加密。
 在这里插入图片描述

Session在分布式情况下失效

因为在分布式环境下,Session访问服务器是随机的,可能出现第一次Sesssion保存A服务器,但是第二次访问的是B服务器。就出现Session失效的情况。
解决方案:
1、启用Session,利用Cookie代替Session
2、自定义Session,通过缓存模拟Session,例如ehache,redis等。但是不太稳定。

可以使用以下代码实现基于cookiesession的购物车功能: 1. 创建一个名为ShoppingCart的Java类,用于存储购物车中的商品信息。 public class ShoppingCart { private Map<Integer, Integer> items; public ShoppingCart() { items = new HashMap<Integer, Integer>(); } public void addItem(int productId, int quantity) { if (items.containsKey(productId)) { int currentQuantity = items.get(productId); items.put(productId, currentQuantity + quantity); } else { items.put(productId, quantity); } } public void removeItem(int productId) { items.remove(productId); } public Map<Integer, Integer> getItems() { return items; } public int getItemCount() { int count = 0; for (int quantity : items.values()) { count += quantity; } return count; } public double getTotalPrice() { double totalPrice = 0; for (Map.Entry<Integer, Integer> entry : items.entrySet()) { int productId = entry.getKey(); int quantity = entry.getValue(); double price = getProductPrice(productId); totalPrice += price * quantity; } return totalPrice; } private double getProductPrice(int productId) { // TODO: 根据商品ID获取商品价格 return 0; } } 2. 在Servlet中使用ShoppingCart类来实现购物车功能。 public class ShoppingCartServlet extends HttpServlet { private static final String CART_ATTRIBUTE_NAME = "cart"; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); ShoppingCart cart = (ShoppingCart) session.getAttribute(CART_ATTRIBUTE_NAME); if (cart == null) { cart = new ShoppingCart(); session.setAttribute(CART_ATTRIBUTE_NAME, cart); } String action = request.getParameter("action"); if (action != null) { switch (action) { case "add": int productId = Integer.parseInt(request.getParameter("productId")); int quantity = Integer.parseInt(request.getParameter("quantity")); cart.addItem(productId, quantity); break; case "remove": int productId = Integer.parseInt(request.getParameter("productId")); cart.removeItem(productId); break; } } request.setAttribute("cart", cart); request.getRequestDispatcher("/shoppingCart.jsp").forward(request, response); } } 3. 在JSP页面中显示购物车信息。 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>购物车</title> </head> <body> <h1>购物车</h1> <table> <thead> <tr> <th>商品ID</th> <th>商品名称</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach var="item" items="${cart.items}"> <tr> <td>${item.key}</td> <td>${getProductName(item.key)}</td> <td>${getProductPrice(item.key)}</td> <td>${item.value}</td> <td>${getProductPrice(item.key) * item.value}</td> <td><a href="?action=remove&productId=${item.key}">删除</a></td> </tr> </c:forEach> </tbody> <tfoot> <tr> <td colspan="4">总计:</td> <td>${cart.totalPrice}</td> <td></td> </tr> </tfoot> </table> </body> </html> 注意:在实际开发中,需要根据具体的业务需求来完善购物车功能,例如:添加商品时需要判断库存是否充足,结算时需要进行支付等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值