servlet实现购物车(项目代码)
程序员文章站
2022-06-21 15:33:25
...
注意:本博客是购物车代码参考,没有使用数据库,也没有JSP,是完完全全的servlet代码的实现
文件名:CartServlet.java
这个里面存放的主要是加入购物车的操作。
具体实现的功能在代码中解释。
package com.sk.shopping;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sk.shopping.dao.DataMoniDao;
import com.sk.shopping.entity.Goods;
/**
* 加入购物车操作
* @author lx
*/
public class CartServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 全部商品
List<Goods> data = DataMoniDao.getData();
// 响应设置
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
// 获取加入购物车商品的信息
int id = Integer.parseInt(request.getParameter("id").toString());
Goods good = new Goods(id,"",0.0,"");
for(Goods g : data) {
if(g.compareTo(good) == 1) {
good = g ;
break;
}
}
// 将商品信息加入cookies
Cookie cookie = new Cookie("cookie_order_" + good.getId() , good.getId() + "");
// 设置cookie的生命周期:
// 如果此处不写 , 将写入浏览器临时cookie中,无法存在客户端本地cookie文件
cookie.setMaxAge(36000);
response.addCookie(cookie);
// 完成了cookie操作,还要进行持久化操作,在学习了jdbc之后加入本功能
// 持久化操作包括:更新购物车信息: 包括 商品总价、商品数量、详细信息等等...
/* ... 此处省略代码30000行... */
// 重定向到当前页
response.sendRedirect(this.getServletContext().getContextPath() + "/index");
// 请求转发
// request.getRequestDispatcher("/index").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
文件名:IndexServlet.java
实现的主要功能为:商品的展示页面。
package com.sk.shopping;
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;
import com.sk.shopping.dao.DataMoniDao;
import com.sk.shopping.entity.Goods;
/**
* 商品展示页面
* --- 本页面为测试cookie功能的入口页面
* @author lx
*/
public class IndexServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
StringBuffer sf = new StringBuffer();
sf.append("<html>");
sf.append("<head>");
sf.append("<title>商品选购页面</title>");
sf.append("<script type=\"text/javascript\">");
sf.append("function suc(){alert(\"添加成功!\")}");
sf.append("</script>");
sf.append("</head>");
sf.append("<body>");
sf.append("<h2>这里是商品选购页面</h2>");
sf.append("<table border='1'>");
sf.append("<tr>");
sf.append("<td>商品编号</td><td>商品名</td><td>图片</td><td>价格</td><td>操作</td>");
sf.append("</tr>");
List<Goods> list = DataMoniDao.getData();
for(int i = 0 ; i < list.size(); i ++){
Goods goods = list.get(i);
sf.append("<tr>");
sf.append("<td>" + goods.getId() + "</td>");
sf.append("<td>" + goods.getName() + "</td>");
sf.append("<td><img src=\"" + goods.getImg_url() + "\" height=\"60px\"></td>");
sf.append("<td>" + goods.getPrice() + "</td>");
sf.append("<td><a onclick=\"suc()\" href=\"cart?id=" + goods.getId() + "\">加入购物车</a> <a href=\"cart\">直接购买</a></td>");
sf.append("</tr>");
}
sf.append("</table>");
sf.append("<a href=\"look\">查看我的购物车</a>");
sf.append("</body>");
sf.append("</html>");
out.write(sf.toString());
out.close();
}
}
文件名:LookShoppingCartServlet.java
主要实现功能:购物车的展示页面。
package com.sk.shopping;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sk.shopping.dao.DataMoniDao;
import com.sk.shopping.entity.Goods;
/**
* 购物车展示页面
* --- 本页面将在cookie文件中取出之前存放的值
* @author Administrator
*
*/
public class LookShoppingCartServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Goods> list = new ArrayList<Goods>();
list = DataMoniDao.getData();
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
StringBuffer sf = new StringBuffer();
sf.append("<html>");
sf.append("<head>");
sf.append("<title>购物车页面</title>");
sf.append("</head>");
sf.append("<body>");
sf.append("<h2>这里是购物车页面</h2>");
sf.append("<table border='1'>");
sf.append("<tr>");
sf.append("<td>商品名</td><td>图片</td><td>数量</td><td>总价格</td><td>操作</td>");
sf.append("</tr>");
// 遍历cookie中存储的商品信息,依次展示到购物车页面中
if(cookies != null) {
for(Cookie cookie : cookies ){
// 获取cookie名,进行判断,确定是购物车应用的cookie信息 , 此处省略...
String cookie_name = cookie.getName();
String cookie_val = cookie.getValue();
int id = 0;
if(!"".equals(cookie_val)){
id = Integer.parseInt(cookie_val);
}
// 取出商品信息
Goods goods = null ;
for(Goods gs : list) {
if(gs.getId() == id) {
goods = gs ;
break;
}
}
if(goods != null) {
sf.append("<tr>");
sf.append("<td>" + goods.getName() + "</td>");
sf.append("<td><img src=\"" + goods.getImg_url() + "\" height=\"60px\"></td>");
// 数量暂时写死,将太多数据存入cookie不安全
sf.append("<td>1</td>");
// 总价要根据数量计算,此处只取单价
sf.append("<td>" + goods.getPrice() + "</td>");
// 结算按钮暂不制作,有兴趣的同学可以继续制作
sf.append("<td><a href=\"javascript:void(0)\">结算</a></td>");
sf.append("</tr>");
}
}
} else {
sf.append("<tr>");
sf.append("<td colspan = '5'><span style='color:red'>您还没有选择商品!</span></td>");
sf.append("</tr>");
}
sf.append("</table>");
sf.append("</body>");
sf.append("</html>");
out.write(sf.toString());
out.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
文件名:Goods.java
主要功能:商品实体类
package com.sk.shopping.entity;
/**
* 商品实体类
* @author lx
*/
public class Goods implements Comparable<Goods> {
private int id ; // 商品编号
private String name ; // 商品名称
private double price ; // 单价
private String img_url ; // 图片地址
public Goods(int id, String name, double price, String img_url) {
this.id = id;
this.name = name;
this.price = price;
this.img_url = img_url;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String getImg_url() {
return img_url;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", img_url=" + img_url + "]";
}
/**
* 重写compareTo方法重新实现排序规则
*/
@Override
public int compareTo(Goods o) {
int result = 0;
result = this.id == o.id ? 1 : 0;
return result;
}
}
文件名:DataMoniDao.java
功能:模拟持久化层的操作。
package com.sk.shopping.dao;
import java.util.ArrayList;
import java.util.List;
import com.sk.shopping.entity.Goods;
/**
* 模拟持久化层的操作
* @author lx
*/
public class DataMoniDao {
/**
* 提供一静态方法getData() 模拟查询出数据库中所有商品信息。。。
* @return 商品信息集
*/
public static List<Goods> getData() {
List<Goods> list = new ArrayList<Goods>();
Goods goods1 = new Goods(1,"霓裳羽衣",53.5,"img/yf1.jpg");
Goods goods2 = new Goods(2,"飞龙道袍",45.5,"img/yf2.jpg");
Goods goods3 = new Goods(3,"软猬甲",80.5,"img/yf3.jpg");
list.add(goods1);
list.add(goods2);
list.add(goods3);
return list ;
}
}
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<!-- 商品选购页面配置 -->
<servlet>
<servlet-name>pageIndex</servlet-name>
<servlet-class>com.sk.shopping.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>pageIndex</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cart</servlet-name>
<servlet-class>com.sk.shopping.CartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cart</servlet-name>
<url-pattern>/cart</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>look</servlet-name>
<servlet-class>com.sk.shopping.LookShoppingCartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>look</servlet-name>
<url-pattern>/look</url-pattern>
</servlet-mapping>
</web-app>
上一篇: python 银行平均等待时间
下一篇: FORM