package cn.zzk.servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import cn.zzk.dao.ProductDao;
import cn.zzk.po.Page;
import cn.zzk.po.Product;
/**
* Servlet implementation class ProductServlet
*/
@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
ProductDao productDao = new ProductDao();
public ProductServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String method = request.getParameter("method");
switch (method) {
case "all":
queryAllProduct(request, response);
break;
case "add":
addProduct(request, response);
break;
case "del":
delProduct(request, response);
break;
case "update":
updateProduct(request, response);
break;
case "byid":
queryProductById(request, response);
break;
case "batchDel":
batchDelProduct(request, response);
break;
case "byPage":
queryProductByPage(request,response);
break;
}
}
private void queryProductByPage(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//得到当前页数
String currPage=request.getParameter("currentPage");
int currentPage=1;
if(currPage!=null)
{
System.out.println(currPage);
currentPage=Integer.parseInt(currPage);
}
//得到总记录数量
int totalCount=0;
List<Product> products=productDao.queryAllProducts();
if(products!=null)
{
totalCount=products.size();
}
//页面大小——每页显示几条记录
int pageSize=5;
//创建页处理对象
Page page=new Page(currentPage,pageSize,totalCount);
//得到当前页显示的记录索引(从0开始)
int beginIndex=page.getBeginIndex();
//得到当前页的商品列表
List<Product> productByPage = productDao.queryProductByPage(beginIndex, pageSize);
//将商品信息和页对象放入request作用域,供页面展示使用
request.setAttribute("productList", productByPage);
request.setAttribute("page", page);
//转向页面
request.getRequestDispatcher("/product/m_productMain.jsp").forward(request, response);
}
private void queryAllProduct(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Product> products = productDao.queryAllProducts();
request.setAttribute("productList", products);
request.getRequestDispatcher("/ProductServlet?method=byPage").forward(request, response);
}
// 增加商品(涉及上传图片文件)
private void addProduct(HttpServletRequest request,
HttpServletResponse response) throws IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// 判断是带文件上传的表单?还是普通表单
boolean isuploadSuccess = false; // 设置上传成功的标志
File imageFile = null; // 图片文件对象
String proname = null; // 商品名称
String pronumber = null; // 商品数量
String proprice = null; // 商品价格
String prosupplier = null; // 商品供应商
String propic = null; // 商品的图片文件名
String destFileName = null; // 重新组织的商品文件名
try {
String uploaddir = request.getServletContext().getRealPath(
"/images");
// 设置上传路径字符串
File upFile = new File(uploaddir); // 创建路径对应的文件对象
if (!upFile.exists()) { // 如果路径不存在,就逐级创建
upFile.mkdirs();
}
if (isMultipart == true) { // 若是带文件上传的表单,继续
FileItemFactory factory = new DiskFileItemFactory(); // 创建文件项工厂
ServletFileUpload upload = new ServletFileUpload(factory); // 创建文件上传对象
List<FileItem> items; // 表单项列表
items = upload.parseRequest(request); // 解析请求对象,得到各个表单项
// 对表单里的各个项目进行分析:
// 如果是普通文本框,则取得其字段名称,根据名称获得其值
// 如果是文件上传项,则取得文件名,对文件名进行处理,然后上传
Iterator<FileItem> it = items.iterator();
while (it.hasNext()) { // 单文件上传
FileItem item = (FileItem) it.next();
// 处理普通文本
String fileldName = item.getFieldName(); // 得到域名称
switch (fileldName) // 根据名称取得其数据,由二进制数据转换成UTF-8编码
{
case "proname":
proname = new String(item.getString().getBytes(
"iso-8859-1"), "utf-8");
break;
case "proprice":
proprice = new String(item.getString().getBytes("iso-8859-1"), "utf-8");
【标题解析】 "java web 网上商城,简单的后台管理商品" 这个标题表明我们关注的是一个基于Java Web技术开发的电子商务平台。它特别强调了后台的商品管理功能,意味着该系统允许管理员轻松地添加、编辑和删除商品信息。 【描述分析】 描述中提到"简单的商城系统,java web",这暗示了该系统是用Java语言和Web技术构建的,可能基于Servlet和JSP等核心技术。"简易的后台管理商品"说明后台界面设计简洁,易于操作,方便管理员进行商品库存、价格等信息的维护。"前台加入购物车,立即购买"意味着前端用户界面具备基本的购物功能,用户可以将商品添加到购物车并直接完成购买流程。"适合二次开发,可运行,带数据库"提示该系统不仅能够直接运行,还为开发者提供了扩展和定制的可能,同时它依赖于数据库来存储商品数据和交易信息。 【标签关联】 "Java web 网上商城 mvc" 这些标签进一步揭示了系统的架构和技术栈。"Java web"再次确认了使用Java进行Web开发的事实,而"mvc"指的是模型-视图-控制器设计模式,这是Java Web应用中常见的开发模式,用于分离业务逻辑、用户界面和数据管理。 【可能涉及的知识点】 1. **Java Web基础**:包括Servlet、JSP、JavaBean等,它们构成了Java Web应用的基础。 2. **Spring MVC框架**:可能是这个项目采用的MVC实现,Spring MVC是Spring框架的一部分,用于处理Web请求,提供模型绑定、数据验证等功能。 3. **数据库管理**:可能使用MySQL、Oracle等关系型数据库,用于存储商品信息、用户信息、订单等数据。 4. **JDBC**:Java Database Connectivity,用于连接和操作数据库。 5. **HTML/CSS/JavaScript**:前端网页的构建技术,用于展示商品、购物车和购买页面。 6. **Ajax**:用于实现前端页面的异步更新,比如购物车数量的实时更改。 7. **RESTful API**:可能采用了RESTful风格的接口设计,使得前后端分离更清晰。 8. **Session/Cookie管理**:用于跟踪用户购物车状态和登录信息。 9. **安全性**:可能涉及到HTTPS、CSRF令牌、XSS防护等安全措施。 10. **Tomcat服务器**:作为Java Web应用的部署环境。 11. **版本控制**:如Git,用于团队协作和代码管理。 12. **构建工具**:如Maven或Gradle,用于自动化构建和依赖管理。 13. **测试**:JUnit或Mockito等工具进行单元测试,确保功能的正确性。 这样的网上商城系统是一个典型的Java Web项目实例,涵盖了从后端数据处理到前端用户体验的完整流程,对于学习和实践Java Web开发来说,是一个很好的学习资源。通过深入研究和二次开发,开发者可以提升自己的技能,并理解一个实际的电商系统是如何运作的。


























































































































- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
- 甜甜不加糖2023-07-25虽然这个文件并不是特别复杂,但它提供了一种简单而高效的方式来管理商品,值得一试。
- 啊看看2023-07-25这个文件的示例代码结构清晰,易于扩展和修改,适合于商城类项目。
- 蓝洱2023-07-25对于初学者来说,这个文件非常适合入门学习Java web开发,逻辑清晰容易理解。
- Xhinking2023-07-25这个文件提供了一个简单而实用的后台管理商品的解决方案,方便快捷。
- 老许的花开2023-07-25在这个文件中,我找到了一些有关商品管理的实用技巧,对我的项目很有帮助。

- 粉丝: 35
- 资源: 2
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 小天公益V14(1.21更新).zip
- 独家解析:2024年新算法在CEC2018测试集上的表现及评价指标输出,独家揭秘:2024年新算法狂欢,十大顶尖算法在CEC2018测试集的实战表现及评价指标揭晓,独家发布10个2024年新算法跑CE
- 2024年全新十大算法,专场测试CEC2021测试集:苦鱼、冠豪猪、美洲狮等算法的实践与评价指标输出,独家解析:2024年新算法全覆盖,CEC2021测试集的全面评估与输出-涵盖苦鱼优化算法、冠豪猪
- ICEEMDAN算法:EMD的升级版信号处理策略,通过自适应噪声添加提高分解精确性与稳定性,“ICEEMDAN算法:基于EMD改进的信号处理高级方法,有效分解复杂信号为内在模式函数”,05ICEE
- 迅雷-chapter-chap14.rar
- 基于Simulink仿真的交错并联Boost PFC电路模型分析与优化-输出电压外环与电感电流内环双闭环控制策略的应用与效果,基于Simulink仿真的交错并联Boost PFC电路模型:双闭环控制
- 迅雷-chapter-chap15.rar
- 深度学习时间序列预测模型-基于TCN-Attention-Bilstm的Matlab回归预测框架及其实证分析,深度学习时间序列预测模型:基于TCN-Attention-Bilstm的Matlab代码
- 基于MATLAB的单侧电源三段式距离保护控制系统的原理、仿真与结果分析,基于MATLAB的单侧电源三段式距离保护控制系统设计与分析:详细原理及仿真结果探讨,基于matlab的单侧电源三段式距离保护控制
- 迅雷-matlab-chapter-CH02.rar
- 无人机 - 适用于大型树木的花园喷雾器
- 《分布式模型预测控制:多智能体点对点转换编队策略文档与程序》-基于4智能体与8智能体的应用研究及参考文献分析,分布式模型预测控制下的多智能体编队系统:点对点转换的协同算法与4、8智能体案例研究,多
- 基于MATLAB的GA优化算法在车间调度问题中的应用:多工件多工序最优加工时间求解,基于MATLAB的GA优化算法在车间调度问题中的应用:多工件多工序在限定条件下的最优调度方案研究,基于matlab的
- 基于IEEE 33节点配电网的Simulink模型:详实数据、多参考文献,支持潮流计算与风光并网研究,基于IEEE 33节点配电网的Simulink模型构建:详细节点与支路数据参考及来源,支持潮流计算
- 基于二自由度模型的SAE坐标系下三自由度汽车操纵侧翻模型仿真研究及理论分析文档,《三自由度汽车操纵侧翻模型仿真:理论分析、建立及仿真结果探讨》,三自由度汽车操纵侧翻模型仿真 有说明文档 学习资料:附带
- 《Chris详解蓝牙射频芯片仿真设计全流程:低功耗蓝牙低噪放、混频器、功放等知识详解,电路版图后仿技术探讨》,《Chris详解蓝牙射频芯片仿真设计全流程:低功耗蓝牙低噪放、混频器、功放等知识要点》,c


