Product类:
package Session;
public class Product {
private int id;
private String name;
private String describe;
private float price;
public Product(int id,String name, String describe, float price) {
super();
this.id=id;
this.name = name;
this.describe = describe;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
productItem类:
package Session;
/*
* 存放购买的物品
* 作为List的基本对象存储,并存储在session中
*/
public class productItem {
Product product;
private int quantity;
public productItem(Product product, int quantity) {
super();
this.product = product;
this.quantity = quantity;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
ShopingServlet类:
package Session;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
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;
/*
* 主要分为三部分
* 第一部分:展示产品
* 第二部分:产品导航
* 第三部分:购物车展示
*/
@WebServlet(name="ShopingServlet",urlPatterns= {"/products","/pref","/cart"})
public class ShopingServlet extends HttpServlet{
//存放商城所有的商品列表
List<Product> products=new ArrayList<Product>();
//设置session中的唯一属性名
private static final String AtrributeName="Cart";
/*
* 初始化产品信息
*
*/
@Override
public void init() throws ServletException {
//添加商品
Product p1=new Product(1, "键盘", "明松数码专营店", 123);
Product p2=new Product(2, "鼠标", "沸腾时代专营店", 234);
Product p3=new Product(3, "运动鞋","森林巨人专卖店", 333);
Product p4=new Product(4,"篮球","乔丹专卖店",890);
//添加到列表
products.add(p1);
products.add(p2);
products.add(p3);
products.add(p4);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("请求中");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//获得其请求
String select=req.getRequestURI();
//判断请求的目的
if(select.endsWith("/products")) { //浏览产品
sentProductList(resp);
}else if(select.endsWith("/pref")){ //产品导航
ShowProductDetial(req,resp);
}else if(select.endsWith("/cart")){ //购物车展示
cart(req,resp);
}else {
System.out.println("GG!!"); //网页地址不存在
}
}
/**
* 产品浏览
* 首页面
*/
private void sentProductList(HttpServletResponse resp) throws IOException {
//获取打印对象
PrintWriter pw=resp.getWriter();
//文档输出部分
pw.println("<html><head></head><body>");
pw.println("**网购商城:");
pw.println("<ul>");
for (Product product : products) {
pw.println("<li>"+product.getName()+"<a href='pref?id="+product.getId()+"'>---->enter</a>"+"</li>");//附带相关产品具体信息的链接
}
pw.println("</ul><a href='cart'> 购物车</a>");
pw.println("</body></html>");
}
/**
* 展示产品具体信息
*
*/
private void ShowProductDetial(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//在URL中解析获取相关产品的id
String productId=req.getParameter("id");
System.out.println(productId);
int id=0;
try {
id=Integer.parseInt(productId);
System.out.println(id);
} catch (Exception e) {
System.out.println("解析异常!!!");
}
//所要展示的商品
Product product=null;
//查询相关产品
for (Product it : products) {
if(id==it.getId()) { //找到要查询的商品
product=it;
break;
}
}
/**
* 购买商品表单
*/
//进入产品详情
//获取打印对象
PrintWriter pw=resp.getWriter();
pw.println("<html><head></head><body>");
pw.println("<input type='hidden' name='id' value="+product.getId()+">"); //通过隐藏域进行会话
pw.println("<ul><li>");
pw.println("name:"+product.getName()+"</li>");
pw.println("<li>describe:"+product.getDescribe()+"</li>");
pw.println("<li>price:¥"+product.getPrice()+"</li>");
pw.println("</ul><form method='post'>");
//表单部分
pw.println("quantity:<input name='num' type='text'>");
pw.println("<input type='submit' value='确定'></form><br/>");
pw.println("<a href='products'>back</a>");
pw.println("</body></html>");
}
/**
* 购买表单后处理
* 将选取的商品放到购物车
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//在URL中解析获取相关产品的id
String productId=req.getParameter("id");
int id=0;
try {
id=Integer.parseInt(productId);
System.out.println(id);
} catch (Exception e) {
System.out.println("解析异常!!!");
}
//所要展示的商品
Product product=null;
//查询相关产品
for (Product it : products) {
if(id==it.getId()) { //找到要查询的商品
product=it;
break;
}
}
int num=0;
num=Integer.parseInt(req.getParameter("num"));
//对象
productItem item=null;
item=new productItem(product, num);
//获取Session对象
HttpSession session=req.getSession();
@SuppressWarnings("unchecked")
List<productItem> cart=(List<productItem>) session.getAttribute(this.AtrributeName);
if(cart==null) { //第一次添加项目
cart=new ArrayList<productItem>();
}
//设置编码方式
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//printWriter
PrintWriter pw=resp.getWriter();
pw.println("<html><head></head><body>");
if(num>0) {
cart.add(item);
session.setAttribute(this.AtrributeName,cart);
//提交后进行返回
//获取打印对象
pw.println(" 购买成功!!!<br/>");
}else {
pw.println(" 购买失败!!!<br/> ");
}
pw.println("<a href='products'>back</a>");
pw.println("</body></html>");
}
/**
* 购物车展示
*/
private void cart(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取Sesssion对象
HttpSession session=req.getSession();
List<productItem>products=(List<productItem>) session.getAttribute(this.AtrributeName);
//获取打印对象
PrintWriter pw=resp.getWriter();
pw.println("<html><head></head><body><ul>");
if(products!=null) {
for (productItem it : products) {
pw.println("<li>"+it.product.getName()+":"+it.getQuantity()+"</li>");
}
}else {
pw.println("<li>NO select products!!!</li>");
}
pw.println("</ul>"
+"<a href='products'> back</a>"
+ "</body></html>");
}
}