1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>库存商品管理系统</title> 8 </head> 9 <body> 10 <h2 align="center">库存商品管理系统</h2> 11 <div align="center"> 12 13 <div class="a"> 14 <a href="add.jsp">商品信息添加</a> 15 </div> 16 <div class="a"> 17 <a href="ProductServlet?method=list">商品信息修改</a> 18 </div> 19 <div class="a"> 20 <a href="del.jsp">商品信息删除</a> 21 </div> 22 <div class="a"> 23 <a href="search.jsp">商品信息查询</a> 24 </div> 25 26 </div> 27 28 </body> 29 </html>
主页面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>添加物资页面</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message");//放置一个字符串,并取出 12 if(message!=null && !"".equals(message)){ 13 14 %> 15 <script type="text/javascript"> 16 alert("<%=request.getAttribute("message")%>"); 17 </script> 18 <%} %> 19 <div align="center"> 20 <h1 style="color: black;">商品信息录入</h1> 21 <a href="index.jsp">返回主页</a> 22 <form action="ProductServlet?method=add" method="post" onsubmit="return check()"> 23 <div class="a"> 24 商品名称<input type="text" id="spmc" name="spmc"/> 25 </div> 26 <div class="a"> 27 生产厂家<input type="text" id="sccj" name="sccj" /> 28 </div> 29 <div class="a"> 30 型号<input type="text" id="xh" name="xh" /> 31 </div> 32 <div class="a"> 33 规格<input type="text" id="gg" name="gg" /> 34 </div> 35 <div class="a"> 36 <button type="submit" class="b">保 存</button> 37 </div> 38 </form> 39 </div> 40 <script type="text/javascript"> 41 function check() { 42 var spmc = document.getElementById("spmc");; 43 var sccj = document.getElementById("sccj"); 44 var xh = document.getElementById("xh"); 45 var gg = document.getElementById("gg"); 46 //非空 47 if(spmc.value == '') { 48 alert('商品名称不能为空'); 49 spmc.focus(); 50 return false; 51 } 52 if(sccj.value == '') { 53 alert('生产厂家不能为空'); 54 sccj.focus(); 55 return false; 56 } 57 if(xh.value == 0) { 58 alert('型号不能为空'); 59 xh.focus(); 60 return false; 61 } 62 if(gg.value == '') { 63 alert('规格不能为空'); 64 gg.focus(); 65 return false; 66 } 67 } 68 </script> 69 </body> 70 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 Object message = request.getAttribute("message"); 12 if(message!=null && !"".equals(message)){ 13 14 %> 15 <script type="text/javascript"> 16 alert("<%=request.getAttribute("message")%>"); 17 </script> 18 <%} %> 19 <div align="center"> 20 <h1 style="color: black;">物资信息删除</h1> 21 <a href="index.jsp">返回主页</a> 22 <form action="ProductServlet?method=getproductbyname" method="post" onsubmit="return check()"> 23 <div class="a"> 24 商品名称<input type="text" id="spmc" name="spmc"/> 25 </div> 26 <div class="a"> 27 <button type="submit" class="b">查 找</button> 28 </div> 29 </form> 30 </div> 31 <script type="text/javascript"> 32 function check() { 33 var spmc = document.getElementById("spmc");; 34 35 //非空 36 if(spmc.value == '') { 37 alert('商品名称不能为空'); 38 spmc.focus(); 39 return false; 40 } 41 } 42 </script> 43 </body> 44 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 .tb, td { 19 border: 1px solid black; 20 font-size: 22px; 21 } 22 </style> 23 </head> 24 <body> 25 <div align="center"> 26 <h1 style="color: black;">商品信息删除</h1> 27 <a href="index.jsp">返回主页</a> 28 <table class="tb"> 29 <tr> 30 <td>商品名称</td> 31 <td>${product.spmc}</td> 32 </tr> 33 <tr> 34 <td>生产厂家</td> 35 <td>${product.sccj}</td> 36 </tr> 37 <tr> 38 <td>型号</td> 39 <td>${product.xh}</td> 40 </tr> 41 <tr> 42 <td>规格</td> 43 <td>${product.gg}</td> 44 </tr> 45 </table> 46 <div class="a"> 47 <a onclick="return check()" href="ProductServlet?method=del&id=${product.id}">删 除</a> 48 </div> 49 </div> 50 <script type="text/javascript"> 51 function check() { 52 if (confirm("确定删除吗?")){ 53 return true; 54 }else{ 55 return false; 56 } 57 } 58 </script> 59 60 </body> 61 </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <div align="center"> <h1 style="color: black;">商品信息删除</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>商品名称</td> <td>${product.spmc}</td> </tr> <tr> <td>生产厂家</td> <td>${product.sccj}</td> </tr> <tr> <td>型号</td> <td>${product.xh}</td> </tr> <tr> <td>规格</td> <td>${product.gg}</td> </tr> </table> <div class="a"> <a onclick="return check()" href="ProductServlet?method=del&id=${product.id}">删 除</a> </div> </div> <script type="text/javascript"> function check() { if (confirm("确定删除吗?")){ return true; }else{ return false; } } </script> </body> </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 Object message = request.getAttribute("message"); 13 if(message!=null && !"".equals(message)){ 14 15 %> 16 <script type="text/javascript"> 17 alert("<%=request.getAttribute("message")%>"); 18 </script> 19 <%} %> 20 <div align="center"> 21 <h1 style="color: black;">商品信息列表</h1> 22 <a href="index.jsp">返回主页</a> 23 <table class="tb"> 24 <tr> 25 <td>id</td> 26 <td>商品名称</td> 27 <td>生产厂家</td> 28 <td>型号</td> 29 <td>规格</td> 30 <td align="center" colspan="2">操作</td> 31 </tr> 32 <c:forEach items="${products}" var="item"> 33 34 <tr> 35 <td>${item.id}</td> 36 <td>${item.spmc}</td> 37 <td>${item.sccj}</td> 38 <td>${item.xh}</td> 39 <td>${item.gg}</td> 40 <td><a href="ProductServlet?method=getproductbyid&id=${item.id}">修改</a></td> 41 </tr> 42 </c:forEach> 43 </table> 44 </div> 45 </body> 46 </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: black;">商品信息列表</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>商品名称</td> <td>生产厂家</td> <td>型号</td> <td>规格</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${products}" var="item"> <tr> <td>${item.id}</td> <td>${item.spmc}</td> <td>${item.sccj}</td> <td>${item.xh}</td> <td>${item.gg}</td> <td><a href="ProductServlet?method=getproductbyid&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 Object message = request.getAttribute("message"); 13 if(message!=null && !"".equals(message)){ 14 15 %> 16 <script type="text/javascript"> 17 alert("<%=request.getAttribute("message")%>"); 18 </script> 19 <%} %> 20 <div align="center"> 21 <h1 style="color: black;">商品信息列表</h1> 22 <a href="index.jsp">返回主页</a> 23 <table class="tb"> 24 <tr> 25 <td>id</td> 26 <td>商品名称</td> 27 <td>生产厂家</td> 28 <td>型号</td> 29 <td>规格</td> 30 <td align="center" colspan="2">操作</td> 31 </tr> 32 <c:forEach items="${products}" var="item"> 33 34 <tr> 35 <td>${item.id}</td> 36 <td>${item.spmc}</td> 37 <td>${item.sccj}</td> 38 <td>${item.xh}</td> 39 <td>${item.gg}</td> 40 <td><a href="ProductServlet?method=getproductbyid&id=${item.id}">修改</a></td> 41 </tr> 42 </c:forEach> 43 </table> 44 </div> 45 </body> 46 </html>
package com.demo.DB; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DB { public static String db_url = "jdbc:mysql://localhost:3307/kucunguanli"; public static String db_user = "root"; public static String db_pass = "320775mxlxjbfl"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package com.demo.entity; public class Product { int id; private String spmc; private String sccj; private String xh; private String gg; public String getspmc() { return spmc; } public void setspmc(String spmc) { this.spmc = spmc; } public String getsccj() { return sccj; } public void setsccj(String sccj) { this.sccj = sccj; } public String getxh() { return xh; } public void setxh(String xh) { this.xh = xh; } public String getgg() { return gg; } public void setgg(String gg) { this.gg = gg; } public void setId(int id) { this.id = id; } public int getId() { return id; } public Product(int id, String spmc, String sccj, String xh,String gg) { this.id = id; this.spmc = spmc; this.sccj = sccj; this.xh = xh; this.gg = gg; } public Product(String spmc, String sccj, String xh,String gg) { this.spmc = spmc; this.sccj = sccj; this.xh = xh; this.gg = gg; } }
package com.demo.servlet; import java.io.IOException; 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 com.demo.Dao.ProductDao; import com.demo.entity.Product; /** * Servlet implementation class ProductServlet */ @WebServlet("/ProductServlet") public class ProductServlet extends HttpServlet { private static final long serialVersionUID = 1L; ProductDao dao = new ProductDao(); /** * @see HttpServlet#HttpServlet() */ public ProductServlet() { super(); // TODO Auto-generated constructor stub } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getproductbyid".equals(method)) { getProductById(req, resp); } else if ("getproductbyname".equals(method)) { getProductByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } private void list(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); List<Product> products = dao.list(); req.setAttribute("products", products); req.getRequestDispatcher("list.jsp").forward(req, resp); } private void getProductByName(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String spmc = req.getParameter("spmc"); Product product = dao.getProductByName(spmc); if(product == null) { req.setAttribute("message", "查无此商品!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("product",product); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } private void getProductById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Product product = dao.getProductById(id); req.setAttribute("product", product); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String spmc = req.getParameter("spmc"); String sccj = req.getParameter("sccj"); String xh = req.getParameter("xh"); String gg = req.getParameter("gg"); List<Product> products = dao.search(spmc,sccj, xh,gg); if(products!=null) { req.setAttribute("products", products); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } else { req.setAttribute("message", "查无此商品信息!"); } } private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String spmc = req.getParameter("spmc"); String sccj = req.getParameter("sccj"); String xh = req.getParameter("xh"); String gg = req.getParameter("gg"); Product product = new Product(id, spmc, sccj,xh,gg); dao.update(product); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("ProductServlet?method=list").forward(req,resp); } private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); dao.delete(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String spmc = req.getParameter("spmc"); String sccj = req.getParameter("sccj"); String xh = req.getParameter("xh"); String gg = req.getParameter("gg"); Product product = new Product(spmc,sccj, xh,gg); if(dao.add(product)) { req.setAttribute("message", "添加成功!"); req.getRequestDispatcher("add.jsp").forward(req, resp); }else { req.setAttribute("message", "添加失败!"); req.getRequestDispatcher("add.jsp").forward(req, resp); } } }
package com.demo.servlet; import java.io.IOException; 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 com.demo.Dao.ProductDao; import com.demo.entity.Product; /** * Servlet implementation class ProductServlet */ @WebServlet("/ProductServlet") public class ProductServlet extends HttpServlet { private static final long serialVersionUID = 1L; ProductDao dao = new ProductDao(); /** * @see HttpServlet#HttpServlet() */ public ProductServlet() { super(); // TODO Auto-generated constructor stub } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getproductbyid".equals(method)) { getProductById(req, resp); } else if ("getproductbyname".equals(method)) { getProductByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } private void list(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); List<Product> products = dao.list(); req.setAttribute("products", products); req.getRequestDispatcher("list.jsp").forward(req, resp); } private void getProductByName(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String spmc = req.getParameter("spmc"); Product product = dao.getProductByName(spmc); if(product == null) { req.setAttribute("message", "查无此商品!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("product",product); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } private void getProductById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Product product = dao.getProductById(id); req.setAttribute("product", product); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String spmc = req.getParameter("spmc"); String sccj = req.getParameter("sccj"); String xh = req.getParameter("xh"); String gg = req.getParameter("gg"); List<Product> products = dao.search(spmc,sccj, xh,gg); if(products!=null) { req.setAttribute("products", products); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } else { req.setAttribute("message", "查无此商品信息!"); } } private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String spmc = req.getParameter("spmc"); String sccj = req.getParameter("sccj"); String xh = req.getParameter("xh"); String gg = req.getParameter("gg"); Product product = new Product(id, spmc, sccj,xh,gg); dao.update(product); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("ProductServlet?method=list").forward(req,resp); } private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); dao.delete(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String spmc = req.getParameter("spmc"); String sccj = req.getParameter("sccj"); String xh = req.getParameter("xh"); String gg = req.getParameter("gg"); Product product = new Product(spmc,sccj, xh,gg); if(dao.add(product)) { req.setAttribute("message", "添加成功!"); req.getRequestDispatcher("add.jsp").forward(req, resp); }else { req.setAttribute("message", "添加失败!"); req.getRequestDispatcher("add.jsp").forward(req, resp); } } }