浅谈Session并且实现购物车

Session

Session(会话机制),会话机制其实就是一个信息共享域,可以保留客户端每次发送请求的信息

Session和Attribute

浅谈Session并且实现购物车

  • 由于Http协议是无状态的,只会保留当前请求,也就是再获取请求2的时候,请求1就会丢失,这也就是用attribute属性
  • 所以有了Session机制,所以客户端每次发送请求,都会存在一个Session里面,类似与键值对
  • 客户端是根据每个Session各自的ID找到相应的属性
  • Session每个客户端只有一个,但可以有很多键值对
  • 在jsp里面,Session会自动创建,然而Sevlet却不会

Session常用方法

HttpSession session=request.getSession(false);
HttpSession session=request.getSession();
System.out.println("创建时间:"+session.getCreationTime());
System.out.println("ID:"+session.getId());
System.out.println("是否是新的Session:"+session.isNew());
System.out.println("上次使用时间:"+session.getLastAccessedTime());
//session.invalidate();
System.out.println("判断是否被销毁:"+session==null);
  • getSession(false),是不会自动创建,也就是如果之前没有session就不会返回,然而getSession()就是会自动创建
  • invalidate()销毁session
  • 同一个Session的ID和创建时间都不变
  • Session的默认存活时间是30分钟,可以自己设置时间

Session如何找寻

  • 每次Session创建都会自动生成ID
  • ID随着response发送给客户端
  • 客户端根据获取的ID,然后访问服务器的Session,所以请求不会过期

Session小Demo,购物车实现

浅谈Session并且实现购物车
这是一个简单的实现原理流程图,下面就更具这个上代码

Login

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("Goods");
	}

由于只是演示购物车机制,所以前面的登录之类的并没有去实现,前面博客有介绍

Goods

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Car> list=new ArrayList<>();
		
		Car car1=new Car(1,"帅帅哒",30+"");
		Car car2=new Car(2,"美",20+"");
		Car car3=new Car(3,"美美哒",10+"");
		Car car4=new Car(4,"帅",40+"");
		
		list.add(car1);
		list.add(car2);
		list.add(car3);
		list.add(car4);
		
		request.setAttribute("list", list);
		request.getRequestDispatcher("goods.jsp").forward(request, response);
		
	}

同样这里仅仅只是演示,没有连接数据库,前面博客都有相关步骤的讲解

shoppingCar

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		String price=request.getParameter("price");
		Car car=new Car(id,name,price);
		HttpSession session=request.getSession();
		List<Car> list=(List<Car>) session.getAttribute("car");
		if(list==null)
			list=new ArrayList<Car>();
		list.add(car);
		session.setAttribute("car", list);
		response.sendRedirect("shoppingCar.jsp");
	}
  • 这里的步骤如下
  • 首先获取到需要操作的商品类,并且实例一个对象
  • 先从session中取出之前保留的购物车,然后判断
  • 如果第一次操作,因为没有商品,所以需要重新创建
  • 然后购物车再添加本次操作的对象
  • 然后吧新的列表添加进去
  • 如果用数据库会更方便,直接从数据库中的表查询即可

Jsp页面

<!-- goods.jsp-->
 <body>
	<table>
		<tr>
			<td>ID</td>
			<td>名字</td>
			<td>价格</td>
			<td>操作</td>
		</tr>
		<% List<Car> list=(List)request.getAttribute("list");
			for(Car car:list){
		%>
		<tr>
			<td><%=car.getId() %></td>
			<td><%=car.getName() %></td>
			<td><%=car.getPrice() %></td>
			<td><a href="ShoppingCar?id=<%=car.getId() %>&name=<%= URLEncoder.encode( car.getName() ,"utf-8")%>&price=<%=car.getPrice() %>">购买</a></td>
		</tr>
		<%} %>
	</table>
</body>

<!--shoppingcar.jsp-->
<a href="Login">返回</a>
<table>
		<tr>
			<td>ID</td>
			<td>名字</td>
			<td>价格</td>
			<td>数量</td>
		</tr>
		<% List<Car> list=(List)session.getAttribute("car");
			for(Car car:list){
		%>
		<tr>
			<td><%=car.getId() %></td>
			<td><%=car.getName() %></td>
			<td><%=car.getPrice() %></td>
			<td>1</td>
		</tr>
		<%} %>
	</table>
</body>

总结

  • 结合本篇博客以及前几篇博客,相信大家写一个带登录注册以及列表管理和添加购物车的后台信息还是没问题的
  • 源码链接