WEB项目-session(简单的购物车的实现)

Session简介

session是服务器端技术,服务器在运行时可以为每一个用户的浏览器创建一个独享的session对象,由于session为用户浏览器独享,所以用户在访问服务器的web资源时,可以把各自的数据存放在各自的session中,当用户再去访问服务器中其他的web资源时,其他web资源再从用户的session中取出数据为用户服务。

Session对象的API

session是一个域对象

request.getSession()  获取session对象

Object getAttribute(String name)  获取域对象中的属性值

void setAttribute(String name,Object value)  设置域对象中的属性值

void removeAttribute(String name)  删除域对象中的属性值

String getId()  获取session的id值

void invalidate()  手动销毁session(用户退出操作)

购物车的简单实现

servlet

package com.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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;
/**
 * 购物车的后台
 * @author 58351
 *
 */
@WebServlet("/cart")
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 0、Map<String,Integer> cart 车里面放商品的名称和数量
		 * 1、接收参数,接收的id值,把id值转换为对应的商品
		 * 2、判断是否是第一次购买(从session中获取购物车,看购物车是否为null)
		 *   如果是第一次,创建一个购物车对象,把商品和数量存入到购物车,再把车存入到session
		 *   如果不是,判断车中是否包含该商品,如果包含,数量+1,如果不包含,将该商品存入到购物车中
		 * 3、转发或者重定向到继续购物或者结算页面
		 * 4、结算,将商品的名称和数量显示到页面上
		 */
		String id = request.getParameter("id");
		Integer index = Integer.parseInt(id);
		String[] ids = new String[]{"手电筒","电视","冰箱","洗衣机","电话","电脑"};
		String name = ids[index-1];
		
		HttpSession session = request.getSession();
		
		Map<String,Integer> cart = (Map<String, Integer>) session.getAttribute("cart");
		if(cart==null){
			cart = new HashMap<String,Integer>();
			cart.put(name, 1);
			session.setAttribute("cart", cart);
		}else{
			if(cart.containsKey(name)){
				Integer count = cart.get(name);
				count++;
				cart.put(name, count);
				session.setAttribute("cart", cart);
			}else{
				cart.put(name, 1);
				session.setAttribute("cart", cart);
			}
		}
		response.sendRedirect("/ShoppingCart/session/gopay.jsp");
	}

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

}

3个JSP页面

cartlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>手电筒<a href="/ShoppingCart/cart?id=1">加入购物车</a></h3>
	<h3>电视<a href="/ShoppingCart/cart?id=2">加入购物车</a></h3>
	<h3>冰箱<a href="/ShoppingCart/cart?id=3">加入购物车</a></h3>
	<h3>洗衣机<a href="/ShoppingCart/cart?id=4">加入购物车</a></h3>
	<h3>电话<a href="/ShoppingCart/cart?id=5">加入购物车</a></h3>
	<h3>电脑<a href="/ShoppingCart/cart?id=6">加入购物车</a></h3>
</body>
</html>
gopay.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3><a href="/ShoppingCart/session/cartlist.jsp">继续购物</a>|<a href="/ShoppingCart/session/pay.jsp">结算</a></h3>
</body>
</html>
pay.jsp
<%@page import="java.util.Set"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>购物清单</h3>
	<%
	Map<String,Integer> cart = (Map<String,Integer>)request.getSession().getAttribute("cart");
	if(cart!=null){
	Set<String> set = cart.keySet();
		for(String name:set){
	%>
	<h2>购买的物品是:<%= name%> 数量为:<%=cart.get(name) %></h2>		
	<%
		}
	}else{
	%>
	<h3>购物车为空,<a href="/ShoppingCart/session/cartlist.jsp">继续购物</a></h3>
	<% 
	}
	%>
</body>
</html>

session的创建和销毁

session的创建:第一次访问的时候,request.getSession();

session的销毁:

1、非正常原因关闭服务器

2、session的默认销毁时间(30分钟)

3、手动销毁session    void invalidate();

猜你喜欢

转载自blog.csdn.net/Tommy5553/article/details/86160021
今日推荐