关于对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();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected 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");
}
}