Tomcat+JavaWeb实现简易商品管理系统(干货满满~)

举报
Bug 终结者 发表于 2023/02/01 11:13:54 2023/02/01
【摘要】 Tomcat+JavaWeb实现简易商品管理系统

系统概述

商品管理系统,商品有商品id,商品名称,商品价格,商品颜色,商品重量,功能有:添加商品,查看商品,修改商品,删除商品。

添加商品

搭建添加商品页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html >
<html>
<head>
<meta charset="utf-8">
<title>新增商品</title>
<style type="text/css">
	input{
		outline: none;
	}
	.btn-show{
		text-decoration: none;
		color:#666;
		display: inline-block;
		padding:10px;
		background-color: #DFF0D8;
		border-radius:5px;
		font-size:14px;
	}
	.btn-submit{
		border:none;
		padding:10px;
		border-radius:5px;
		width: 60px;
		background-color: #DFF0D8;
		color:#666;
	}
	.btn-show:hover{
		background-color: #DFF0E8;
	}
</style>
</head>
<body>
	<form action="/GoodsMannagerSystem/addGoods" method="post">
		<h3>新增商品</h3>
		<p>&emsp;Id<input type="text" name="id" required="required"  autocomplete="off">
		</p>
		<p>
			商品名称:<input type="text" name="goodsname" required="required" autocomplete="off">
		</p>
		<p>
			商品价格:<input type="number" name="goodsprice" required="required" autocomplete="off" >
		</p>
		<p>
			商品颜色:<input type="text" name="goodscolor" required="required" autocomplete="off">
		</p>
		<p>
			商品重量:<input type="number" name="goodsweight" required="required" autocomplete="off">
		</p>
		<input class="btn-submit" type="submit" value="提交">&emsp;<a class="btn-show" href="/GoodsMannagerSystem/showGoods">查看商品信息</a>
	</form>
</body>
</html>

页面效果:
在这里插入图片描述

实现添加商品功能

新建商品类:

public class Goods implements Serializable{

	private int id;
	private String goodsName;
	private double goodsPrice;
	private String goodsColor;
	private double goodsWeight;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getGoodsName() {
		return goodsName;
	}
	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}
	public double getGoodsPrice() {
		return goodsPrice;
	}
	public void setGoodsPrice(double goodsPrice) {
		this.goodsPrice = goodsPrice;
	}
	public String getGoodsColor() {
		return goodsColor;
	}
	public void setGoodsColor(String goodsColor) {
		this.goodsColor = goodsColor;
	}
	public double getGoodsWeight() {
		return goodsWeight;
	}
	public void setGoodsWeight(double goodsWeight) {
		this.goodsWeight = goodsWeight;
	}
	
	
}

由于输入输出文件使用频繁,所以单独新建类存放从文件内读出数据与写入数据功能
该类具体业务代码如下:

public class GoodsService {

	//文件地址
	public static final String filePath = "C://Users//Desktop//goods.txt";
	
	/**
	 * 增加商品
	 * @param goodsName
	 * @param id
	 * @param goodsPrice
	 * @param goodsColor
	 * @param goodsWeight
	 */
	public static void addGoods(String goodsName, int id, double goodsPrice, 
			String goodsColor, double goodsWeight) {
		ArrayList<Goods> goods = readGoods();
		Goods good = new Goods();
		good.setId(id);
		good.setGoodsName(goodsName);
		good.setGoodsPrice(goodsPrice);
		good.setGoodsColor(goodsColor);
		good.setGoodsWeight(goodsWeight);
		goods.add(good);
		saveGoods(goods);
	}
	
	/**
	 * 从文件中读出商品集合并返回该集合
	 * @return
	 */
	public static ArrayList<Goods> readGoods() {
		FileInputStream fr = null;
		ArrayList<Goods> goods = new ArrayList<>();
		try {
			fr = new FileInputStream(filePath);
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			ObjectInputStream ois = new ObjectInputStream(fr);
			goods = (ArrayList<Goods>) ois.readObject();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return goods;
	}
	
	/**
	 * 将商品集合写入文件
	 * @param goods
	 */
	public static void saveGoods(ArrayList<Goods> goods) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(filePath);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(goods);
			oos.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

创建AddGoodsServlet类实现添加商品:

@WebServlet("/addGoods")
public class AddGoods extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/goods/index.jsp").forward(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=utf-8");
		String goodsName = req.getParameter("goodsname");
		String strGoodsPrice = req.getParameter("goodsprice");
		String goodsColor = req.getParameter("goodscolor");
		String strGoodsWeight = req.getParameter("goodsweight");
		Double goodsPrice = Double.valueOf(strGoodsPrice);
		Double goodsWeight = Double.valueOf(strGoodsWeight);
		String strId = req.getParameter("id");
		Integer id = Integer.valueOf(strId);
		System.out.println(goodsName);
		GoodsService.addGoods(goodsName,id, goodsPrice, goodsColor, goodsWeight);
		
		String method = req.getMethod();
		String URI = req.getRequestURI();
		String URL = req.getRequestURL().toString();
		//web应用名称
		String contextPath = req.getContextPath();
		//获得访问的客户端IP地址
		String remoteAddr = req.getRemoteAddr();
		
		req.setAttribute("method", method);
		req.setAttribute("URI", URI);
		req.setAttribute("URL", URL);
		req.setAttribute("contextPath", contextPath);
		req.setAttribute("remoteAddr", remoteAddr);
		
		resp.sendRedirect("/GoodsMannagerSystem/addGoods");
		
		System.out.println("添加成功~");
	}
}

查看商品
新建商品页

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html >
<html>
<head>
<meta charset="utf-8">
<title>商品列表</title>
<style type="text/css">
	.btn{
		text-decoration: none;
		color:#666;
		display: inline-block;
		padding:5px;
		background-color: #DFF0D8;
		font-size:14px;
	}
	table tr th{
		padding: 0 20px 0 0;
	}
	.clear::after{
		content: "";
		display: block;
		clear: both;
	}
	.box h3{
		float:left;
	}
	.box a{
		margin: 20px 0 0 50px;
		border-radius:5px;
	}
</style>
</head>
<body>
	<div class="box clear">
		<h3>商品列表信息</h3>
		<a class="btn btn-add" href="/GoodsMannagerSystem/addGoods">新增商品</a>
	</div>
	<table>
	<tr>
		<th>商品名称</th>
		<th>商品价格</th>
		<th>商品颜色</th>
		<th>商品重量</th>
		<th>操作</th>
	</tr>
		<c:forEach items="${goods }" var="g">
			<tr><td>${g.goodsName }</td>
			<td>${g.goodsPrice }</td>
			<td>${g.goodsColor }</td>
			<td>${g.goodsWeight }</td>
			<td><a class="btn btn-del" href="/GoodsMannagerSystem/deleteGoods?id=${g.id }">删除</a></td>
			<td><a class="btn btn-edit" href="/GoodsMannagerSystem/ToUpdateServlet?id=${g.id }">修改</a></td></tr>
		</c:forEach>
	</table>
	
	
</body>
</html>

新建ShowListServlet类用于获取商品数据并传入页面显示:

@WebServlet("/showGoods")
public class ListGoods extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<Goods> goods = GoodsService.readGoods();
		req.setAttribute("goods", goods);
		req.getRequestDispatcher("/WEB-INF/goods/showList.jsp").forward(req, resp);
	}
}

新建修改页:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html >
<html>
<head>
<meta charset="utf-8">
<title>修改用户</title>

<style type="text/css">
	.btn{
		padding:10px;
		background-color: #DFF0E8;
		border:none;
		color:#666;
		border-radius:5px;
	}
</style>
</head>
<body>

	<form action="/GoodsMannagerSystem/updates">
		<h3>修改商品信息</h3>
		<p>
			<input type="hidden" name="id" autocomplete="off" value="${goods.id }" placeholder="${goods.id }">
		</p>
		<p>
			商品名称:<input type="text" name="goodsname" autocomplete="off" value="${goods.goodsName }" placeholder="${goods.goodsName }">
		</p>
		<p>
			商品价格:<input type="number" name="goodsprice" autocomplete="off" value="${goods.goodsPrice }" placeholder="${goods.goodsPrice }" >
		</p>
		<p>
			商品颜色:<input type="text" name="goodscolor"   autocomplete="off" value="${goods.goodsColor }" placeholder="${goods.goodsColor }">
		</p>
		<p>
			商品重量:<input type="number" name="goodsweight" autocomplete="off" value="${goods.goodsWeight }" placeholder="${goods.goodsWeight }">
		</p>
		<input class="btn btn-submit" type="submit" value="提交">&emsp;<input class="btn btn-submit" type="reset" value="重置">
		
	</form>
</body>
</html>

新建ToUpdateServlet类,将要修改的原始数据传入修改页

@WebServlet("/ToUpdateServlet")
public class ToUpdateServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String inId = req.getParameter("id");
		int id = Integer.parseInt(inId);
		ArrayList<Goods> goods = GoodsService.readGoods();
		for (Goods goods2 : goods) {
			if (goods2.getId() == id) {
				req.setAttribute("goods", goods2);
				break;
			}
		}
		req.getRequestDispatcher("/WEB-INF/goods/update.jsp").forward(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

新建DoUpdateServlet类,将修改完毕的数据传入页面显示

@WebServlet("/updates")
public class DoUpdateServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String inId = req.getParameter("id");
		System.out.println(inId);
		int id = Integer.valueOf(inId);
		String goodsName = req.getParameter("goodsname");
		String strGoodsPrice = req.getParameter("goodsprice");
		String goodsColor = req.getParameter("goodscolor");
		String strGoodsWeight = req.getParameter("goodsweight");
		Double goodsPrice = 0.0;
		if (strGoodsPrice != null)
			goodsPrice = Double.valueOf(strGoodsPrice);
		Double goodsWeight = 0.0;
		if (strGoodsWeight != null)
			goodsWeight = Double.valueOf(strGoodsWeight);
		ArrayList<Goods> goods = GoodsService.readGoods();
		for (Goods goods2 : goods) {
			if (goods2.getId() == id) {
				goods2.setGoodsName(goodsName);
				goods2.setGoodsPrice(goodsPrice);
				goods2.setGoodsColor(goodsColor);
				goods2.setGoodsWeight(goodsWeight);
				break;
			}
		}
		GoodsService.saveGoods(goods);
		resp.sendRedirect("/GoodsMannagerSystem/showGoods");
	}
}


新建DeleteServlet类用于删除数据

@WebServlet("/deleteGoods")
public class DeleteServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String strid = req.getParameter("id");
		System.out.println(strid);
		int id = Integer.parseInt(strid);
		ArrayList<Goods> goods = GoodsService.readGoods();
		for (Goods goods2 : goods) {
			if (goods2.getId() == id) {
				goods.remove(goods2);
				break;
			}
		}
		GoodsService.saveGoods(goods);
		
		resp.sendRedirect("showGoods");
	}
}

上效果
打开新增页面:加入2条数据
在这里插入图片描述
查看商品列表信息
在这里插入图片描述
点击修改第一条信息
在这里插入图片描述
修改价格为3599,并提交
在这里插入图片描述
自动跳转到商品列表,显示已成功修改
删除第一条信息
在这里插入图片描述
ok,测试没问题,如有我未发现的小bug,望各位大佬评论区留言,代码之路艰辛坎坷,我们并肩作战,加油!

难点:修改与删除,思路:传入商品id并循环遍历商品集合拿到数据,进行相应的操作。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。