javaee之session的购物车练习
2016-07-22 23:45
405 查看
通过面向对象的分层和设计:
一、实体层
import java.io.Serializable;
public class Product implements Serializable {
private int id; //商品的id
private String name; //商品的名称
private int num = 1; //商品的数量
private double price; //商品的价格
private String desc; //商品的描述
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Product(int id, String name, double price, String desc) {
super();
this.id = id;
this.name = name;
this.price = price;
this.desc = desc;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
}
二、业务层
import java.util.ArrayList;
import java.util.List;
public class ProductDao {
private static List<Product> list = new ArrayList<Product>();
static {
list.add(new Product(1,"三国演义",100,"基友日记"));
list.add(new Product(2,"西游记",100,"人畜日记"));
list.add(new Product(3,"水浒传",100,"兄弟日记"));
list.add(new Product(4,"红楼梦",100,"娘炮日记"));
}
public List<Product> findAll() {
// TODO Auto-generated method stub
return list;
}
public Product findById(int id){
for(Product p : list){
if(p.getId()==id)
return p;
}
return null;
}
}
三、服务层和视图层
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddcarServlet extends HttpServlet {
private ProductDao dao = new ProductDao();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//根据id得到所选择的标签
String id = request.getParameter("id");
Product product = dao.findById(Integer.parseInt(id));
//保存在session中
HttpSession session = request.getSession();
Map<Integer,Product> car = (Map<Integer,Product>) session.getAttribute("car");
//第一次添加的时候要创建
if(car ==null){
car = new HashMap<Integer,Product>();
}
//计算添加到购物车的商品的数量
if(car != null && car.containsKey(Integer.parseInt(id))){
Product p = car.get(Integer.parseInt(id));
p.setNum(p.getNum()+1);
}else{
//没买过的时候,需要新加入购物车
car.put(product.getId(), product);
}
session.setAttribute("car", car);
//4.回显
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(product.getName()+"已经成功加入购物车!<a href='"+request.getContextPath()+"/ShowCarServlet'>查看购物车</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
通过对session的购物车练习,明白了对session会话管理的基础应用
一、实体层
import java.io.Serializable;
public class Product implements Serializable {
private int id; //商品的id
private String name; //商品的名称
private int num = 1; //商品的数量
private double price; //商品的价格
private String desc; //商品的描述
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Product(int id, String name, double price, String desc) {
super();
this.id = id;
this.name = name;
this.price = price;
this.desc = desc;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
}
二、业务层
import java.util.ArrayList;
import java.util.List;
public class ProductDao {
private static List<Product> list = new ArrayList<Product>();
static {
list.add(new Product(1,"三国演义",100,"基友日记"));
list.add(new Product(2,"西游记",100,"人畜日记"));
list.add(new Product(3,"水浒传",100,"兄弟日记"));
list.add(new Product(4,"红楼梦",100,"娘炮日记"));
}
public List<Product> findAll() {
// TODO Auto-generated method stub
return list;
}
public Product findById(int id){
for(Product p : list){
if(p.getId()==id)
return p;
}
return null;
}
}
三、服务层和视图层
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AddcarServlet extends HttpServlet {
private ProductDao dao = new ProductDao();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//根据id得到所选择的标签
String id = request.getParameter("id");
Product product = dao.findById(Integer.parseInt(id));
//保存在session中
HttpSession session = request.getSession();
Map<Integer,Product> car = (Map<Integer,Product>) session.getAttribute("car");
//第一次添加的时候要创建
if(car ==null){
car = new HashMap<Integer,Product>();
}
//计算添加到购物车的商品的数量
if(car != null && car.containsKey(Integer.parseInt(id))){
Product p = car.get(Integer.parseInt(id));
p.setNum(p.getNum()+1);
}else{
//没买过的时候,需要新加入购物车
car.put(product.getId(), product);
}
session.setAttribute("car", car);
//4.回显
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(product.getName()+"已经成功加入购物车!<a href='"+request.getContextPath()+"/ShowCarServlet'>查看购物车</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import java.util.Map.Entry; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class ShowCarServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); Map<Integer,Product> map = (Map<Integer, Product>) session.getAttribute("car"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String html = ""; html +="<table border='1' width='500px'>"; html +="<caption>当前购物车的商品:</caption>"; html += "<tr><th>编号</th><th>商品名称</th><th>数量</th><th>价格</th><th>描述</th><th>小计</th></tr>"; double totalPrice = 0; for( Entry<Integer, Product> set : map.entrySet()){ Product p = set.getValue(); html += "<tr><td>"+p.getId()+"</td><td>"+p.getName()+"</td><td>"+p.getNum()+"</td><td>"+p.getPrice()+"元</td><td>"+p.getDesc()+"</td><td>"+p.getNum()*p.getPrice()+"元</td></tr>"; totalPrice+=p.getNum()*p.getPrice(); } html+= "<tr><td colspan='6' align='right'>合计:"+totalPrice+"元</td></tr>"; html +="</table>"; out.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ShowServlet extends HttpServlet { private ProductDao dao = new ProductDao(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //先得到列表中的数据 List<Product> list = dao.findAll(); //显示数据 response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String html = ""; html +="<table border='1' width='500px'>"; html += "<tr><th>编号</th><th>商品名称</th><th>价格</th><th>描述</th><th>操作</th></tr>"; for (Product p : list) { html += "<tr><td>"+p.getId()+"</td><td>"+p.getName()+"</td><td>"+p.getPrice()+"</td><td>"+p.getDesc()+"</td><td><a href='"+request.getContextPath()+"/AddcarServlet?id="+p.getId()+"'>购买</a></td></tr>"; } html +="</table>"; out.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
通过对session的购物车练习,明白了对session会话管理的基础应用
相关文章推荐
- Angularjs 跨域请求
- kindeditor 批量上传 上传失败 thinkphp swfupload session
- 杰奇登录后的东西都是在session里面的
- ASP中SESSION无法保存问题的解决办法
- Oracle中的Connect/session和process的区别及关系介绍
- Node.js编程中客户端Session的使用详解
- 完美解决ajax访问遇到Session失效的问题
- 浅谈COOKIE和SESSION区别
- 解析PHP的session过期设置
- php中Session的生成机制、回收机制和存储机制探究
- php中将一个对象保存到Session中的方法
- 深入解析Session是否必须依赖Cookie
- 对比分析php中Cookie与Session的异同
- PHP Session机制简介及用法
- 新手菜鸟必读:session与cookie的区别
- 深入讲解PHP Session及如何保持其不过期的方法
- PHP Session变量不能传送到下一页的解决方法
- PHP中的session永不过期的解决思路及实现方法分享
- php session实现多级目录存放实现代码