天天看點

淺談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() ," target="_blank" rel="external nofollow" utf-8")%>&price=<%=car.getPrice() %>">購買</a></td>
		</tr>
		<%} %>
	</table>
</body>

<!--shoppingcar.jsp-->
<a href="Login" target="_blank" rel="external nofollow" >傳回</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>
           

總結

  • 結合本篇部落格以及前幾篇部落格,相信大家寫一個帶登入注冊以及清單管理和添加購物車的背景資訊還是沒問題的
  • 源碼連結