Session可以设置名字和值(和Map里的key-value类似)

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.util.Date;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. public class ShowSession extends HttpServlet {
  10.     @Override
  11.     protected void doGet(HttpServletRequest request,
  12.             HttpServletResponse response) throws ServletException, IOException {
  13.         HttpSession session = request.getSession(true);
  14.         PrintWriter pw = response.getWriter();
  15.         response.setContentType("text/html;charset=gb2312");
  16.         String heading = "";
  17.         //从名为"accessCount"的session取值(属性)
  18.         Integer accessCount = (Integer) session.getAttribute("accessCount");
  19.         //若空,则显示访问第一次,否则访问次数自动加1
  20.         if (accessCount == null) {
  21.             accessCount = new Integer(0);
  22.             heading = "New,Welcome!";
  23.         } else {
  24.             accessCount = new Integer(accessCount.intValue() + 1);
  25.             heading = "Welcome,back!";
  26.         }
  27.         session.setAttribute("accessCount", accessCount);
  28.         pw
  29.                 .println("<html>/n<head>/n<title>ShowSession</title>/n</head>"
  30.                         + "<body>"
  31.                         + "<h3 color = 'red' align = 'center'>"
  32.                         + heading
  33.                         + "</h3><br>"
  34.                         + "<table border = 1 align = 'center'>"
  35.                         + "<tr bgcolor = #FF00FF>/n<td>Info Type</td><td>Value</td></tr>"
  36.                         + "<tr><td>SessionID</td><td>" + session.getId()
  37.                         + "</td></tr>" + "<tr><td>Create Time</td><td>"
  38.                         + new Date(session.getCreationTime()) + "</td></tr>"
  39.                         + "<tr><td>Last Access Time</td><td>"
  40.                         + new Date(session.getLastAccessedTime())
  41.                         + "</td></tr>"
  42.                         + "<tr><td>Number of AccessCount</td><td>"
  43.                         + accessCount + "</td></tr>" + "</table></body></html>");
  44.     }
  45.     @Override
  46.     protected void doPost(HttpServletRequest request,
  47.             HttpServletResponse response) throws ServletException, IOException {
  48.         doGet(request, response);
  49.     }
  50. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码实现基于cookie和session的购物车功能: 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、付费专栏及课程。

余额充值