javaweb之session简介,并用session实现购物车的功能
session为每个浏览器独创一个区域,有一个key,通过key可以找到独享的区域
下面这个代码利用的就是session,每个购物车是一个mao集合,然后把这个map集合放到session中作为每个浏览器独享的区域
如果下面代码哪里看不懂欢迎留言!!!
实现效果:
主要分为三个JSP界面,和一个处理继续购物的servlet
Car.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<img class="pg-img">手电筒<a href="/Demo3/Car?id=1">加入购物车</a>
<img class="pg-img">电视<a href="/Demo3/Car?id=2">加入购物车</a>
<img class="pg-img">电脑<a href="/Demo3/Car?id=3">加入购物车</a><br>
<img class="pg-img">冰箱<a href="/Demo3/Car?id=4">加入购物车</a>
<img class="pg-img">洗衣机<a href="/Demo3/Car?id=5">加入购物车</a>
<img class="pg-img">手机<a href="/Demo3/Car?id=6">加入购物车</a>
</body>
</html>
contine.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3><a href="/Demo3/JSP/Car.jsp">继续购物</a><a href="/Demo3/JSP/over.jsp">结算</a></h3>
</body>
</html>
over.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>商品信息</h4>
<%
@SuppressWarnings("unchecked")
Map<String,Integer> car=(Map<String,Integer>)request.getSession().getAttribute("car");
if(car!=null){
Set<String> names=car.keySet();
for(String name:names){
%><h3>商品名字:<%=name %>数量:<%=car.get(name) %></h3><%
}
}else{
%><h3>购物车为空 <a href="/Demo3/JSP/Car.jsp">继续购物</a></h3><%
}
%>
<h3><a href="/Demo3/JSP/Car.jsp">继续购物</a></h3>
</body>
</html>
Car.java
package cn.itcast.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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;
/**
* Servlet implementation class Car
*/
@WebServlet("/Car")
public class Car extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Car() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
//获取点击购物那个商品的ID(String),每次只是点击一个,所以ID就是一个数可以转为int
String id=request.getParameter("id");
String[] names=new String[] {"手电筒","电视","电脑","冰箱","洗衣机","手机",};
int index=Integer.parseInt(id);
String productname=names[index-1];
//上面是将Id和商品的映射关系弄好,有了id也就有了商品的名字
//首先获取一个session对象,MAP就是一个购物车,每次把这个车放到这个session中
HttpSession session=request.getSession();
@SuppressWarnings("unchecked")
Map<String,Integer> car=(Map<String,Integer>)session.getAttribute("car");
if(car==null) {
//如果购物车为空则创建一个map用来存储,放到车里map,然后放到session中
car=new HashMap<String,Integer>();
car.put(productname, 1);
session.setAttribute("car", car);
}else {
if(car.containsKey(productname)) {
//首先要判断是否有,有的话则加一
Integer count=car.get(productname);
count++;
car.put(productname, count);
session.setAttribute("car", car);
}else {
//没有的话数量则从1开始
car.put(productname, 1);
session.setAttribute("car", car);
}
}
//重定向到下一个页面
response.sendRedirect(request.getContextPath()+"/JSP/contine.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}