关于对session的理解通俗语句结合实例
这个其实是 因为在session会创建一个唯一的id号给服务器,当用户请求时 会从服务器中获取id号
注:1.我们可以把session想成map集合
2.有些人对 Map<Integer, CartItem> cart = (Map<Integer, CartItem>) session.getAttribute("cart");
中的getAttribute("cart")里面的cart 不是很理解 先解释如下 其实就是 cart是自己创建的 随意起的名字与任何页面都无关联关系
原因就是刚开始访问网站时会自动生成一个session,这个session为空,所以通过if(cart==null)把我们的sessin里面的东西补充起来
package com.zhiliaotang.servlet;import com.zhiliaotang.model.CartItem;
import com.zhiliaotang.model.Product;
import com.zhiliaotang.service.ProductService;
import com.zhiliaotang.service.impl.ProductServiceImpl;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@WebServlet("/product.do")
public class ProductServlet extends HttpServlet {//调用业务层private ProductService productService = new ProductServiceImpl();@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//设置请求参数编码req.setCharacterEncoding("UTF-8");//获取请求参数String method = req.getParameter("method");if(null==method){//查询所有商品list(req,resp);}else if("view".equals(method)){//查看商品详情view(req,resp);}else if("cart".equals(method)){//加入购物车addCart(req,resp);}}private void addCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取编号String id = req.getParameter("id");//根据编号获取商品对象Product product = productService.findProductById(Integer.parseInt(id));//获取会话HttpSession session = req.getSession();//从会话中获取购物车对象Map<Integer, CartItem> cart = (Map<Integer, CartItem>) session.getAttribute("cart");/*这个其实是 因为在session会创建一个唯一的id号给服务器,当用户请求时 会从服务器中获取id号注:1.我们可以把session想成map集合2.有些人对 Map<Integer, CartItem> cart = (Map<Integer, CartItem>) session.getAttribute("cart");中的getAttribute("cart")里面的cart 不是很理解 先解释如下 其实就是 cart是自己创建的 随意起的名字与任何页面都无关联关系原因就是刚开始访问网站时会自动生成一个session,这个session为空,所以通过if(cart==null)把我们的sessin里面的东西补充起来* */if(cart==null){cart = new HashMap<Integer, CartItem>();//将购物车放入会话中session.setAttribute("cart",cart);}CartItem cartItem = null;//判断购物中是否存在该商品if(cart.containsKey(product.getId())){//存在此商品cartItem = cart.get(product.getId());//累加数据cartItem.setCount(cartItem.getCount()+1);}else{//购物车没有此商品cartItem = new CartItem(product,1);}//将购物车项添加到购物车中cart.put(product.getId(),cartItem);//重定向到购物车页面resp.sendRedirect("product_cart.jsp");}private void view(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//获取编号String id = req.getParameter("id");//根据编号获取商品对象Product product = productService.findProductById(Integer.parseInt(id));//将商品入在请求属性中req.setAttribute("product",product);//页面转发req.getRequestDispatcher("product_view.jsp").forward(req,resp);}private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{//获取会话HttpSession session = req.getSession();List<Product> products = productService.findAllProducts();//将产品集合放入会话中session.setAttribute("products",products);//页面重定向resp.sendRedirect("product_list.jsp");}
}