基于JSP、java、Tomcat三者的项目实战--校园交易平台系统--(实习,答辩皆 ...

打印 上一主题 下一主题

主题 552|帖子 552|积分 1656

技能支持:JAVA、JSP
服务器:TOMCAT 7.0.86
编程软件:IntelliJ IDEA 2021.1.3 x64

全部文件展示



网页实现功能截图



主页


注册 

 登录




购物车主页


修改功能


修改成功

添加商品功能 


添加成功

添加进入购物车功能


付出功能


 付出过的历史清单账单



全部代码


   dao->StudentDAO

  

  1. package dao;
  2. import entiy.Product;
  3. import entiy.Student;
  4. import entiy.Total;
  5. import util.DBUtil;
  6. import java.sql.Connection;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. public class StudentDAO {
  13.     public List<Student> findAll() throws Exception {
  14.         List<Student> students = new ArrayList<Student>();
  15.         Connection conn = null;
  16.         PreparedStatement prep = null;
  17.         ResultSet rst = null;
  18.         try {
  19.             conn = DBUtil.getConnection();
  20.             prep = conn.prepareStatement("select * from users");
  21.             rst = prep.executeQuery();
  22.             while (rst.next()) {
  23.                 int id = rst.getInt("id");
  24.                 String name = rst.getString("name");
  25.                 int idname = rst.getInt("idname");
  26.                 String pd = rst.getString("pd");
  27.                 Student e1 = new Student();
  28.                 e1.setName(name);
  29.                 e1.setIdname(idname);
  30.                 e1.setPd(pd);
  31.                 students.add(e1);
  32.             }
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.             throw e;
  36.         } finally {
  37.             DBUtil.close(conn);
  38.         }
  39.         return students;
  40.     }
  41.     public List<Product> findAllgoods() throws Exception {
  42.         List<Product> products = new ArrayList<Product>();
  43.         Connection conn = null;
  44.         PreparedStatement prep = null;
  45.         ResultSet rst = null;
  46.         try {
  47.             conn = DBUtil.getConnection();
  48.             prep = conn.prepareStatement("select * from goods");
  49.             rst = prep.executeQuery();
  50.             while (rst.next()){
  51.                 int id = rst.getInt("id");
  52.                 String name = rst.getString("name");
  53.                 double price = rst.getDouble("price");
  54.                 Product e1 = new Product();
  55.                 e1.setId(id);
  56.                 e1.setName(name);
  57.                 e1.setPrice(price);
  58.                 products.add(e1);
  59.             }
  60.         } catch (Exception e) {
  61.             e.printStackTrace();
  62.             throw  e;
  63.         }finally {
  64.             DBUtil.close(conn);
  65.         }
  66.         return products;
  67.     }
  68.     public List<Total> findtotals() throws Exception {
  69.         List<Total> totals = new ArrayList<Total>();
  70.         Connection conn = null;
  71.         PreparedStatement prep = null;
  72.         ResultSet rst = null;
  73.         try {
  74.             conn = DBUtil.getConnection();
  75.             prep = conn.prepareStatement("select * from Total");
  76.             rst = prep.executeQuery();
  77.             while (rst.next()){
  78.                 int id = rst.getInt("id");
  79.                 double total = rst.getDouble("total");
  80.                 Total e1 = new Total();
  81.                 System.out.println(e1);
  82.                 e1.setId(id);
  83.                 e1.setTotal(total);
  84.                 totals.add(e1);
  85.             }
  86.         } catch (Exception e) {
  87.             e.printStackTrace();
  88.             throw  e;
  89.         }finally {
  90.             DBUtil.close(conn);
  91.         }
  92.         return totals;
  93.     }
  94.     public void save(Student e) throws Exception {
  95.         Connection conn = null;
  96.         PreparedStatement prep = null;
  97.         try {
  98.             conn = DBUtil.getConnection();
  99.             prep = conn.prepareStatement(
  100.                     "INSERT INTO users (name, idname, pd) VALUES (?, ?, ?)");
  101.             prep.setString(1, e.getName());
  102.             prep.setInt(2, e.getIdname());
  103.             prep.setString(3, e.getPd());
  104.             prep.executeUpdate();
  105.         } catch (Exception e1) {
  106.             e1.printStackTrace();
  107.             throw e1;
  108.         } finally {
  109.             // Close PreparedStatement and Connection
  110.             if (prep != null) {
  111.                 try {
  112.                     prep.close();
  113.                 } catch (SQLException e2) {
  114.                     e2.printStackTrace();
  115.                 }
  116.             }
  117.             DBUtil.close(conn);
  118.         }
  119.     }
  120.     public void savegoods(Product e) throws Exception {
  121.     Connection conn = null;
  122.     PreparedStatement prep = null;
  123.     try {
  124.         conn = DBUtil.getConnection();
  125.         prep = conn.prepareStatement(
  126.                 "INSERT INTO goods (name,price) VALUES (?, ?)");
  127.         prep.setString(1, e.getName());
  128.         prep.setDouble(2, e.getPrice());
  129.         prep.executeUpdate();
  130.     } catch (SQLException e1) {
  131.         // Handle specific SQL exceptions or log them for debugging
  132.         e1.printStackTrace();
  133.         throw new Exception("Failed to save goods: " + e1.getMessage(), e1);
  134.     } finally {
  135.         // Close PreparedStatement and Connection in finally block
  136.         if (prep != null) {
  137.             try {
  138.                 prep.close();
  139.             } catch (SQLException e2) {
  140.                 e2.printStackTrace();
  141.             }
  142.         }
  143.         DBUtil.close(conn);
  144.     }
  145. }
  146.     public void delete(int id) throws Exception {
  147.         Connection conn =null;
  148.         PreparedStatement prep = null;
  149.         try {
  150.             conn = DBUtil.getConnection();
  151.             prep = conn.prepareStatement("delete from emp where id="+id+"");
  152.             prep.executeUpdate();
  153.         }catch (Exception e){
  154.             e.printStackTrace();
  155.             throw e;
  156.         }finally {
  157.             DBUtil.close(conn);
  158.         }
  159.     }
  160.     //根据ID查询商品信息(修改商品信息第一步)
  161.     public Product findById(int id) throws Exception{
  162.         Connection conn = null;
  163.         PreparedStatement prep = null;
  164.         ResultSet rst = null;
  165.         Product e =new Product();
  166.         try {
  167.             conn =DBUtil.getConnection();
  168.             System.out.println(conn);
  169.             prep = conn.prepareStatement(
  170.                     "select * from goods where id="+id+"");
  171.             System.out.println(id);
  172.             rst = prep.executeQuery();
  173.             if(rst.next()){
  174.                 int id1 = rst.getInt("id");
  175.                 String name = rst.getString("name");
  176.                 Double price = rst.getDouble("price");
  177.                 e.setId(id1);
  178.                 e.setName(name);
  179.                 e.setPrice(price);
  180.             }
  181.         }catch (Exception e1){
  182.             e1.printStackTrace();
  183.             throw e1;
  184.         }finally {
  185.             DBUtil.close(conn);
  186.         }
  187.         return e;
  188.     }
  189.     public void Update(int id,String name,double price) throws Exception {
  190.         Connection connection = DBUtil.getConnection();
  191.         PreparedStatement preparedStatement = connection.prepareStatement("update goods set name=?,price=? where id =?;");
  192.         preparedStatement.setString(1,name);
  193.         preparedStatement.setDouble(2,price);
  194.         preparedStatement.setInt(3,id);
  195.         int i = preparedStatement.executeUpdate();
  196.         connection.close();
  197.     }
  198.     public void totalprice(Total e) throws Exception {
  199.         Connection conn = null;
  200.         PreparedStatement prep = null;
  201.         try {
  202.             conn = DBUtil.getConnection();
  203.             prep = conn.prepareStatement(
  204.                     "INSERT INTO Total (total) VALUES (?)");
  205.             prep.setDouble(1, e.getTotal());
  206.             prep.executeUpdate();
  207.         } catch (Exception e1) {
  208.             e1.printStackTrace();
  209.             throw e1;
  210.         } finally {
  211.             // Close PreparedStatement and Connection
  212.             if (prep != null) {
  213.                 try {
  214.                     prep.close();
  215.                 } catch (SQLException e2) {
  216.                     e2.printStackTrace();
  217.                 }
  218.             }
  219.             DBUtil.close(conn);
  220.         }
  221.     }
  222.     public static void main(String[] args) throws Exception {
  223.        /* StudentDAO dao = new StudentDAO();
  224.         Student e = new Student();
  225.         e.setName("ww");
  226.         e.setIdname(Integer.parseInt("123"));
  227.         e.setPd("123");
  228.         dao.save(e);
  229.         System.out.println(e);
  230.         */
  231.     }
  232. }
复制代码

   entity->CarItem ->roduct -> Student ->Total 

  

  1. package entiy;
  2. // 可选的购物车条目类
  3. public class CarItem {
  4.     private Product product;
  5.     private int quantity;
  6.     // 其他属性
  7.     // 构造方法、getter和setter方法
  8.     public CarItem(Product product, int quantity /*, 其他属性 */) {
  9.         this.product = product;
  10.         this.quantity = quantity;
  11.         // 初始化其他属性
  12.     }
  13.     public Product getProduct() {
  14.         return product;
  15.     }
  16.     public void setProduct(Product product) {
  17.         this.product = product;
  18.     }
  19.     public int getQuantity() {
  20.         return quantity;
  21.     }
  22.     public void setQuantity(int quantity) {
  23.         this.quantity = quantity;
  24.     }
  25.     @Override
  26.     public String toString() {
  27.         return "CarItem{" +
  28.                 "product=" + product +
  29.                 ", quantity=" + quantity +
  30.                 '}';
  31.     }
  32. }
复制代码
  1. package entiy;
  2. public class Product {
  3.     private int id;
  4.     private String name;
  5.     private double price;
  6.     // 其他商品属性,如颜色、库存等
  7.     // 构造方法、getter和setter方法
  8.     public int getId() {
  9.         return id;
  10.     }
  11.     public void setId(int id) {
  12.         this.id = id;
  13.     }
  14.     public Product(/*, 其他属性 */) {
  15.         this.name = name;
  16.         this.price = price;
  17.         // 初始化其他属性
  18.     }
  19.     public String getName() {
  20.         return name;
  21.     }
  22.     public void setName(String name) {
  23.         this.name = name;
  24.     }
  25.     public double getPrice() { return price; }
  26.     public void setPrice(double price) {
  27.         this.price = price;
  28.     }
  29.     @Override
  30.     public String toString() {
  31.         return "Product{" +
  32.                 "id=" + id +
  33.                 ", name='" + name + '\'' +
  34.                 ", price=" + price +
  35.                 '}';
  36.     }
  37. }
复制代码
  1. package entiy;
  2. public class Student {
  3.     private String name;
  4.     private int idname;
  5.     private String pd;
  6.     public String getName(){
  7.         return name;
  8.     }
  9.     public void setName(String name){
  10.         this.name=name;
  11.     }
  12.     public int getIdname(){
  13.         return idname;
  14.     }
  15.     public void setIdname(int idname){
  16.         this.idname = idname;
  17.     }
  18.     public String getPd(){
  19.         return pd;
  20.     }
  21.     public void setPd(String pd){
  22.         this.pd = pd;
  23.     }
  24.     @Override
  25.     public String toString(){
  26.         return "Student{"+
  27.                 "name="+name+
  28.                 "idname="+idname+
  29.                 ",pd="+pd+
  30.                 '}';
  31.     }
  32. }
复制代码
  1. package entiy;
  2. public class Total {
  3.     private double total;
  4.     private int id;
  5.     public void setId(int id) {
  6.         this.id = id;
  7.     }
  8.     public int getId() {
  9.         return id;
  10.     }
  11.     public Total(/*, 其他属性 */) {
  12.         this.total = total;
  13.         this.id = id;
  14.         // 初始化其他属性
  15.     }
  16.     public void setTotal(double total) {
  17.         this.total = total;
  18.     }
  19.     public double getTotal() {
  20.         return total;
  21.     }
  22.     @Override
  23.     public String toString() {
  24.         return "Total{" +
  25.                 "total=" + total +
  26.                 ", id=" + id +
  27.                 '}';
  28.     }
  29. }
复制代码

   Servlet包下的许多服务 

  
 以下按顺序分列
  1. package Servlet;
  2. import dao.StudentDAO;
  3. import entiy.Product;
  4. import entiy.Student;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. public class AddShoppingServlet extends HttpServlet {
  12.     public void service(HttpServletRequest request,
  13.                         HttpServletResponse response)
  14.             throws ServletException, IOException {
  15.         request.setCharacterEncoding("utf-8");
  16.         //读取参数
  17.         String name = request.getParameter("name");
  18.         String price = request.getParameter("price");
  19.         response.setContentType("text/html;charset=utf-8");
  20.         PrintWriter out=response.getWriter();
  21.         try {
  22.             StudentDAO dao = new StudentDAO();
  23.             //创建商品对象
  24.             Product e1 = new Product();
  25.             e1.setName(name);
  26.             e1.setPrice(Double.parseDouble(price));
  27.             System.out.println(e1.getName());
  28.             System.out.println(e1.getPrice());
  29.             dao.savegoods(e1);
  30.             //重定向到主页面
  31.             response.sendRedirect("list");
  32.         } catch (Exception e) {
  33.             e.printStackTrace();
  34.             out.println("系统繁忙,请稍后再试!");
  35.         }
  36.         out.close();
  37.     }
  38. }
复制代码
 
  1. package Servlet;
  2. import dao.StudentDAO;
  3. import entiy.Product;
  4. import javax.servlet.RequestDispatcher;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.util.List;
  12. public class listServlet extends HttpServlet {
  13.     public void service(HttpServletRequest request,
  14.                         HttpServletResponse response)
  15.             throws ServletException, IOException {
  16.         response.setContentType("text/html;charset=utf-8");
  17.         PrintWriter out = response.getWriter();
  18.         try {
  19.             StudentDAO dao = new StudentDAO();
  20.             List<Product> products = dao.findAllgoods();
  21.             request.setAttribute("goods",products);
  22.             RequestDispatcher rd = request.getRequestDispatcher("shopping.jsp");
  23.             rd.forward(request,response);
  24.         } catch (Exception e) {
  25.             e.printStackTrace();
  26.             out.println("系统繁忙,请稍后再试!");
  27.         }
  28.     }
  29. }
复制代码
  1. package Servlet;
  2. import java.io.*;
  3. import java.sql.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. public class LoginServlet extends HttpServlet {
  7.     // 数据库连接信息(这里假设你使用MySQL数据库)
  8.     private String jdbcURL = "jdbc:mysql://localhost:3306/sdjyy";
  9.     private String jdbcUsername = "root";
  10.     private String jdbcPassword = "asd123";
  11.     protected void doPost(
  12.             HttpServletRequest request,
  13.             HttpServletResponse response) throws ServletException, IOException {
  14.         // 获取登录页面提交的账号和密码
  15.         String username = request.getParameter("idname");
  16.         String password = request.getParameter("pd");
  17.         System.out.println(username);
  18.         System.out.println(password);
  19.         // 假设这里是你的登录逻辑,可以是数据库验证等
  20.         boolean isValidUser = false;
  21.         try {
  22.             isValidUser = checkUser(username, password);
  23.         } catch (Exception e) {
  24.             e.printStackTrace();
  25.         }
  26.         // 这里进行账号密码验证的逻辑,比如查询数据库
  27.         // 注意:这里应该使用 PreparedStatement 或其他安全方式来避免 SQL 注入攻击
  28.         // 示例中简单输出账号密码到控制台
  29.         System.out.println("Username: " + username);
  30.         System.out.println("Password: " + password);
  31.         if (isValidUser) {
  32.             // 登录成功,重定向到商品页面
  33.             HttpSession session = request.getSession();
  34.             session.setAttribute("username", username);
  35.             response.sendRedirect("list");
  36.         } else {
  37.             // 登录失败,可以返回到登录页面或者给出错误信息
  38.             response.sendRedirect("denglu.jsp?error=1"); // 假设带有错误参数
  39.         }
  40.     }
  41.     // 模拟用户验证,实际情况应根据你的业务逻辑实现
  42.     private boolean checkUser(String username, String password) throws Exception {
  43.         try {
  44.             // 加载数据库驱动程序
  45.             Class.forName("com.mysql.jdbc.Driver");
  46.             // 连接数据库
  47.             Connection connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
  48.             // 查询语句
  49.             String sql = "SELECT * FROM users WHERE idname = ? AND pd = ?";
  50.             PreparedStatement statement = connection.prepareStatement(sql);
  51.             statement.setString(1, username);
  52.             statement.setString(2, password);
  53.             System.out.println(username);
  54.             System.out.println(password);
  55.             // 执行查询
  56.             ResultSet result = statement.executeQuery();
  57.             // 如果查询到结果集,说明用户名和密码匹配
  58.             if (result.next()) {
  59.                 return true;
  60.             }
  61.             // 关闭连接
  62.             connection.close();
  63.         } catch (SQLException | ClassNotFoundException e) {
  64.             e.printStackTrace();
  65.         }
  66.         // 如果没有查询到结果或者出现异常,则验证失败
  67.         return false;
  68.     }
  69. }
复制代码
 
  1. package Servlet;
  2. import dao.StudentDAO;
  3. import entiy.Total;
  4. import javax.servlet.RequestDispatcher;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.util.List;
  12. public class LSQDServlet extends HttpServlet {
  13.     public void service(HttpServletRequest request,
  14.                         HttpServletResponse response)
  15.             throws ServletException, IOException {
  16.         response.setContentType("text/html;charset=utf-8");
  17.         PrintWriter out = response.getWriter();
  18.         try {
  19.             StudentDAO dao = new StudentDAO();
  20.             List<Total> totals = dao.findtotals();
  21.             request.setAttribute("total",totals);
  22.             RequestDispatcher rd = request.getRequestDispatcher("Checkout.jsp");
  23.             rd.forward(request,response);
  24.         } catch (Exception e) {
  25.             e.printStackTrace();
  26.             out.println("系统繁忙,请稍后再试!");
  27.         }
  28.     }
  29. }
复制代码
  1. package Servlet;
  2. import dao.StudentDAO;
  3. import entiy.Product;
  4. import javax.servlet.RequestDispatcher;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. public class SelectShoppingServlet extends HttpServlet {
  12.     public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  13.         request.setCharacterEncoding("utf-8");
  14.         response.setContentType("text/html;charset=utf-8");
  15.         StudentDAO studentDAO = new StudentDAO();
  16.         String id = request.getParameter("id");
  17.         System.out.println(id);
  18.         PrintWriter writer = response.getWriter();
  19.         try {
  20.             Product product = studentDAO.findById(Integer.parseInt(id));
  21.             request.setAttribute("goods",product );
  22.             RequestDispatcher rd = request.getRequestDispatcher("Update.jsp");
  23.             rd.forward(request,response);
  24.         } catch (Exception e) {
  25.             throw new RuntimeException(e);
  26.         }
  27.     }
  28. }
复制代码
  1. package Servlet;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import java.io.*;
  5. import java.sql.*;
  6. public class ShoppingServlet extends HttpServlet {
  7.     // 数据库连接信息(这里假设你使用MySQL数据库)
  8.     private String jdbcURL = "jdbc:mysql://localhost:3306/sdjyy";
  9.     private String jdbcUsername = "root";
  10.     private String jdbcPassword = "asd123";
  11.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  12.             throws ServletException, IOException {
  13.         HttpSession session = request.getSession();
  14.         String idname = (String) session.getAttribute("idname");
  15.         if (idname != null) {
  16.             // 如果成功获取到 idname,可以继续处理逻辑
  17.             // 在这里可以根据 idname 进行数据库查询或者其他业务逻辑
  18.             Connection conn = null;
  19.             PreparedStatement stmt = null;
  20.             ResultSet rs = null;
  21.             try {
  22.                 conn = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
  23.                 String sql = "SELECT * FROM users WHERE idname = ?";
  24.                 stmt = conn.prepareStatement(sql);
  25.                 stmt.setString(1, idname);
  26.                 rs = stmt.executeQuery();
  27.                 if (rs.next()) {
  28.                     // 从结果集中获取其他信息或者执行其他操作
  29.                     String username = rs.getString("idname");
  30.                     System.out.println("Username retrieved: " + username);
  31.                     // 将用户名存储到 session 中(如果需要的话)
  32.                     session.setAttribute("username", username);
  33.                     // 转发到 shopping.jsp 页面或者其他需要的页面
  34.                     RequestDispatcher dispatcher = request.getRequestDispatcher("/shopping.jsp");
  35.                     dispatcher.forward(request, response);
  36.                 } else {
  37.                     // 如果未能从数据库中找到对应的记录,处理逻辑(例如跳转到错误页面)
  38.                     response.sendRedirect("/denglu.jsp");
  39.                 }
  40.             } catch (SQLException se) {
  41.                 se.printStackTrace();
  42.                 response.getWriter().println("数据库连接错误");
  43.             } finally {
  44.                 // 关闭资源
  45.                 try {
  46.                     if (rs != null) rs.close();
  47.                     if (stmt != null) stmt.close();
  48.                     if (conn != null) conn.close();
  49.                 } catch (SQLException se) {
  50.                     se.printStackTrace();
  51.                 }
  52.             }
  53.         } else {
  54.             // 如果未能成功取 idname,处理逻辑(例如跳转到登录页面或者错误页面)
  55.             response.sendRedirect("/denglu.jsp");
  56.         }
  57.     }
  58. }
复制代码
  1. package Servlet;
  2. import dao.StudentDAO;
  3. import entiy.Product;
  4. import entiy.Total;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. public class TotalServlet extends HttpServlet {
  12.     public void service(HttpServletRequest request,
  13.                         HttpServletResponse response)
  14.             throws ServletException, IOException {
  15.         request.setCharacterEncoding("utf-8");
  16.         String total = request.getParameter("total");
  17.         response.setContentType("text/html;charset=utf-8");
  18.         PrintWriter out=response.getWriter();
  19.         try {
  20.             StudentDAO dao = new StudentDAO();
  21.            Total e1 = new Total();
  22.             e1.setTotal(Double.parseDouble(total));
  23.             System.out.println(e1.getTotal());
  24.             dao.totalprice(e1);
  25.             response.sendRedirect("list");
  26.         } catch (Exception e) {
  27.             e.printStackTrace();
  28.             out.println("系统繁忙,请稍后再试!");
  29.         }
  30.         out.close();
  31.     }
  32. }
复制代码
  1. package Servlet;
  2. import dao.StudentDAO;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. public class UpdateShoppingServlet extends HttpServlet {
  9.     public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  10.         StudentDAO productDAO = new StudentDAO();
  11.         request.setCharacterEncoding("utf-8");
  12.         response.setContentType("text/html;charset=utf-8");
  13.         // Get parameters from request
  14.         String idStr = request.getParameter("id");
  15.         String name = request.getParameter("name");
  16.         String priceStr = request.getParameter("price");
  17.         // Validate parameters
  18.         if (idStr == null || name == null || priceStr == null || idStr.isEmpty() || name.isEmpty() || priceStr.isEmpty()) {
  19.             response.getWriter().println("Invalid parameters. Please provide all fields.");
  20.             return;
  21.         }
  22.         try {
  23.             // Parse parameters
  24.             int id = Integer.parseInt(idStr);
  25.             double price = Double.parseDouble(priceStr);
  26.             // Update product in DAO
  27.             productDAO.Update(id, name, price);
  28.             // Redirect to list page after successful update
  29.             response.sendRedirect("list");
  30.         } catch (NumberFormatException e) {
  31.             // Handle if id or price is not a valid number
  32.             response.getWriter().println("Invalid id or price format. Please enter valid numbers.");
  33.         } catch (Exception e) {
  34.             // Handle other exceptions
  35.             throw new ServletException("Error updating product", e);
  36.         }
  37.     }
  38. }
复制代码
  1. package Servlet;
  2. import dao.StudentDAO;
  3. import entiy.Student;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. public class zhuceServlet extends HttpServlet {
  11.     public void service(HttpServletRequest request,
  12.                         HttpServletResponse response)
  13.             throws ServletException, IOException {
  14.         request.setCharacterEncoding("utf-8");
  15.         String name = request.getParameter("name");
  16.         String idname = request.getParameter("idname");
  17.         String pd = request.getParameter("pd");
  18.         response.setContentType("text/html;charset=utf-8");
  19.         PrintWriter out = response.getWriter();
  20.         try {
  21.             if (name == null || idname == null || pd == null ||
  22.                     name.isEmpty() || idname.isEmpty() || pd.isEmpty()) {
  23.                 throw new IllegalArgumentException("参数不能为空");
  24.             }
  25.             StudentDAO dao = new StudentDAO();
  26.             Student e1 = new Student();
  27.             e1.setName(name);
  28.             e1.setIdname(Integer.parseInt(idname));
  29.             e1.setPd(pd);
  30.             dao.save(e1);
  31.             response.sendRedirect("denglu.jsp");
  32.         } catch (NumberFormatException e) {
  33.             out.println("ID必须是数字");
  34.         } catch (IllegalArgumentException e) {
  35.             out.println(e.getMessage());
  36.         } catch (Exception e) {
  37.             e.printStackTrace();
  38.             out.println("系统繁忙,请稍后再试!");
  39.         } finally {
  40.             out.close();
  41.         }
  42.     }
  43. }
复制代码

   util->DBUtil

  1. package util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. public class DBUtil {
  6.     public static Connection getConnection() throws Exception {
  7.         Connection conn = null;
  8.         try {
  9.             Class.forName("com.mysql.jdbc.Driver");
  10.             conn = DriverManager.getConnection(
  11.                     "jdbc:mysql://localhost:3306/sdjyy?" +
  12.                             "useUnicode=true&characterEncoding=utf8","root","asd123"
  13.             );
  14.         } catch (Exception e) {
  15.             e.printStackTrace();
  16.             throw e;
  17.         }
  18.         return conn;
  19.     }
  20.     public static void close(Connection conn){
  21.         if(conn!=null){
  22.             try {
  23.                 conn.close();
  24.             }catch (SQLException e){
  25.                 e.printStackTrace();
  26.             }
  27.         }
  28.     }
  29.     public static void main(String[] args)throws Exception{
  30.         Connection conn = getConnection();
  31.         System.out.println(conn);
  32.     }
  33. }
复制代码
 

   web.xml 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5.          version="4.0">
  6.     <servlet>
  7.         <servlet-name>zhuce</servlet-name>
  8.         <servlet-class>Servlet.zhuceServlet</servlet-class>
  9.     </servlet>
  10.     <servlet-mapping>
  11.         <servlet-name>zhuce</servlet-name>
  12.         <url-pattern>/zhuce</url-pattern>
  13.     </servlet-mapping>
  14.     <servlet>
  15.         <servlet-name>Login</servlet-name>
  16.         <servlet-class>Servlet.LoginServlet</servlet-class>
  17.     </servlet>
  18.     <servlet-mapping>
  19.         <servlet-name>Login</servlet-name>
  20.         <url-pattern>/Login</url-pattern>
  21.     </servlet-mapping>
  22.     <servlet>
  23.         <servlet-name>Shopping</servlet-name>
  24.         <servlet-class>Servlet.ShoppingServlet</servlet-class>
  25.     </servlet>
  26.     <servlet-mapping>
  27.         <servlet-name>Shopping</servlet-name>
  28.         <url-pattern>/Shopping</url-pattern>
  29.     </servlet-mapping>
  30.     <servlet>
  31.         <servlet-name>list</servlet-name>
  32.         <servlet-class>Servlet.listServlet</servlet-class>
  33.     </servlet>
  34.     <servlet-mapping>
  35.         <servlet-name>list</servlet-name>
  36.         <url-pattern>/list</url-pattern>
  37.     </servlet-mapping>
  38.     <servlet>
  39.         <servlet-name>add</servlet-name>
  40.         <servlet-class>Servlet.AddShoppingServlet</servlet-class>
  41.     </servlet>
  42.     <servlet-mapping>
  43.         <servlet-name>add</servlet-name>
  44.         <url-pattern>/add</url-pattern>
  45.     </servlet-mapping>
  46.     <servlet>
  47.         <servlet-name>load</servlet-name>
  48.         <servlet-class>Servlet.SelectShoppingServlet</servlet-class>
  49.     </servlet>
  50.     <servlet-mapping>
  51.         <servlet-name>load</servlet-name>
  52.         <url-pattern>/load</url-pattern>
  53.     </servlet-mapping>
  54.     <servlet>
  55.         <servlet-name>update</servlet-name>
  56.         <servlet-class>Servlet.UpdateShoppingServlet</servlet-class>
  57.     </servlet>
  58.     <servlet-mapping>
  59.         <servlet-name>update</servlet-name>
  60.         <url-pattern>/update</url-pattern>
  61.     </servlet-mapping>
  62.     <servlet>
  63.         <servlet-name>total</servlet-name>
  64.         <servlet-class>Servlet.TotalServlet</servlet-class>
  65.     </servlet>
  66.     <servlet-mapping>
  67.         <servlet-name>total</servlet-name>
  68.         <url-pattern>/total</url-pattern>
  69.     </servlet-mapping>
  70.     <servlet>
  71.         <servlet-name>LSQD</servlet-name>
  72.         <servlet-class>Servlet.LSQDServlet</servlet-class>
  73.     </servlet>
  74.     <servlet-mapping>
  75.         <servlet-name>LSQD</servlet-name>
  76.         <url-pattern>/LSQD</url-pattern>
  77.     </servlet-mapping>
  78. </web-app>
复制代码

JSP代码 

   AddShopping.jsp

  1. <%@page contentType="text/html;charset=utf-8" pageEncoding="utf-8" %>
  2. <%@page import="java.util.*,java.text.*,dao.*" %>
  3. <!DOCTYPE html>
  4. <html lang="en">
  5. <head>
  6.     <meta charset="UTF-8">
  7.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8.     <title>Add Product</title>
  9.     <link rel="stylesheet" href="css/style.css">
  10.     <style>
  11.         /* 这里可以添加页面特定的 CSS 样式 */
  12.         body {
  13.             font-family: Arial, sans-serif;
  14.             background-color: #f0f0f0;
  15.             margin: 0;
  16.             padding: 0;
  17.         }
  18.         #wrap {
  19.             width: 80%;
  20.             margin: 0 auto;
  21.             background-color: #fff;
  22.             box-shadow: 0 0 10px rgba(0,0,0,0.1);
  23.         }
  24.         header {
  25.             background-color: #333;
  26.             color: #fff;
  27.             padding: 10px;
  28.             text-align: center;
  29.         }
  30.         #content {
  31.             padding: 20px;
  32.         }
  33.         .form_table {
  34.             width: 100%;
  35.         }
  36.         .form_table td {
  37.             padding: 10px;
  38.         }
  39.         .inputgri {
  40.             width: 100%;
  41.             padding: 8px;
  42.             border: 1px solid #ccc;
  43.             border-radius: 4px;
  44.             box-sizing: border-box;
  45.             font-size: 14px;
  46.         }
  47.         .button {
  48.             padding: 10px 20px;
  49.             background-color: #4CAF50;
  50.             color: white;
  51.             border: none;
  52.             border-radius: 4px;
  53.             cursor: pointer;
  54.             font-size: 14px;
  55.         }
  56.         .button:hover {
  57.             background-color: #45a049;
  58.         }
  59.         #footer {
  60.             background-color: #333;
  61.             color: #fff;
  62.             text-align: center;
  63.             padding: 10px;
  64.         }
  65.     </style>
  66. </head>
  67. <body>
  68. <div id="wrap">
  69.     <div id="content">
  70.         <h1>添加商品:</h1>
  71.         <form action="add" method="post">
  72.             <table class="form_table">
  73.                 <tr>
  74.                     <td align="right">Name:</td>
  75.                     <td><input type="text" class="inputgri" name="name" /></td>
  76.                 </tr>
  77.                 <tr>
  78.                     <td align="right">Price:</td>
  79.                     <td><input type="text" class="inputgri" name="price" /></td>
  80.                 </tr>
  81.             </table>
  82.             <p><input type="submit" class="button" value="确认添加"  /></p>
  83.         </form>
  84.     </div>
  85.     <footer id="footer">
  86.         sdjyw@126.com
  87.     </footer>
  88. </div>
  89. </body>
  90. </html>
复制代码
   Checkoust.jsp

  1. <%@ page import="entiy.Total" %>
  2. <%@ page import="java.util.*" %>
  3. <%@ page import="java.text.SimpleDateFormat" %>
  4. <%@page contentType="text/html;charset=utf-8" pageEncoding="utf-8" %>
  5. <html>
  6. <head>
  7.     <title>Title</title>
  8.     <style>
  9.         .table {
  10.             width: 100%;
  11.             border-collapse: collapse;
  12.         }
  13.         .table_header {
  14.             background-color: lightgray;
  15.         }
  16.         .table td, .table th {
  17.             border: 1px solid black;
  18.             padding: 8px;
  19.         }
  20.         .button {
  21.             padding: 10px 20px;
  22.             background-color: #4CAF50;
  23.             color: white;
  24.             border: none;
  25.             text-align: center;
  26.             text-decoration: none;
  27.             display: inline-block;
  28.             font-size: 16px;
  29.             cursor: pointer;
  30.         }
  31.     </style>
  32. </head>
  33. <body>
  34. <h1>
  35.     查看您的历史清单
  36. </h1>
  37. <h2>
  38.     <p>
  39.         <%
  40.             Date date = new Date();
  41.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  42.         %>
  43.         <%=sdf.format(date)%>
  44.         <br />
  45.     </p >
  46. </h2>
  47. <table class="table">
  48.     <tr class="table_header">
  49.         <td>
  50.             ID
  51.         </td>
  52.         <td>
  53.             消费金额
  54.         </td>
  55.     </tr>
  56.     <%
  57.         List<Total> totals = (List<Total>) request.getAttribute("total");
  58.         if (totals != null && !totals.isEmpty()) {
  59.             for (int i = 0; i < totals.size(); i++) {
  60.                 Total e = totals.get(i);
  61.     %>
  62.     <tr>
  63.         <td><%= e.getId() %></td>
  64.         <td><%= e.getTotal() %></td>
  65.     </tr>
  66.     <%
  67.         }
  68.     } else {
  69.     %>
  70.     <tr>
  71.         <td colspan="2">没有找到消费金额记录。</td>
  72.     </tr>
  73.     <%
  74.         }
  75.     %>
  76. </table>
  77. <p>
  78.     <input type="button" class="button" value="返回主页面" onclick="location='list'"/>
  79. </p >
  80. </div>
  81. </div>
  82. <div id="footer">
  83.     <div id="footer_bg">
  84.         sdjyy@444.com
  85.     </div>
  86. </div>
  87. </div>
  88. </body>
  89. </html>
复制代码
   denglu.jsp

  1. <%@page contentType="text/html;charset=utf-8" pageEncoding="UTF-8" %>
  2. <%@page import="java.util.*,java.text.*,entiy.*" %>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6.     <title>交易网登录</title>
  7.     <script>
  8.         // 检查是否有错误信息参数,如果有则显示弹框提示
  9.         <% if ("1".equals(request.getParameter("error"))) { %>
  10.         window.onload = function() {
  11.             alert("您的账号或密码输入错误,请重试。");
  12.         }
  13.         <% } %>
  14.     </script>
  15.     <style>
  16.         h1{
  17.             font-size: 40px;
  18.             color: blanchedalmond;
  19.             text-align: center;
  20.             font-family: 'Courier New', Courier, monospace;
  21.             font-style: italic;
  22.         }
  23.     </style>
  24. </head>
  25. <body>
  26. <h1 name="top">交易网</h1>
  27. <table align="center" cellspacing="0">
  28.     <tr>
  29.         <td>
  30.             <table cellpadding="0">
  31.                 <tr>
  32.                     <!-- 使用相对路径引用图片 -->
  33.                     <td><img src="img/3.jpg"></td>
  34.                 </tr>
  35.                 <tr>
  36.                     <td>
  37.                         <ul>
  38.                             <li>全面支持iPhone/iPad及Android等系统</li>
  39.                             <li>客户端、手机与网页,实现发送、阅读邮件立即同步普通登录手机号登录</li>
  40.                         </ul>
  41.                     </td>
  42.                 </tr>
  43.             </table>
  44.         </td>
  45.         <td>
  46.             <table border="1" cellspacing="0">
  47.                 <form action="Login" method="post">
  48.                     <tr>
  49.                         <td>
  50.                             <table cellpadding="30">
  51.                                 <tr>
  52.                                     <td colspan="2" align="center"><strong>登录</strong></td>
  53.                                 </tr>
  54.                                 <tr>
  55.                                     <td>昵称:
  56.                                         <input type="text" id="name" name="name">
  57.                                     </td>
  58.                                 </tr>
  59.                                 <tr>
  60.                                     <td>账号:
  61.                                         <input type="text" id="idname" name="idname">
  62.                                     </td>
  63.                                 </tr>
  64.                                 <tr>
  65.                                     <td>密码:
  66.                                         <input type="password" id="pd" name="pd">
  67.                                     </td>
  68.                                 </tr>
  69.                                 <tr>
  70.                                     <td align="center" colspan="2">
  71.                                         <input type="submit" value="登录" onclick="f1();" />
  72.                                         <!-- 注册按钮改用正确的 <a> 标签 -->
  73.                                         <a href="zhuce.jsp"><input type="button" value="注册" onclick="f2();"></a>
  74.                                     </td>
  75.                                 </tr>
  76.                             </table>
  77.                         </td>
  78.                     </tr>
  79.                 </form>
  80.             </table>
  81.         </td>
  82.     </tr>
  83.     <tr>
  84.         <td align="right">关于交易网</td>
  85.     </tr>
  86. </table>
  87. </body>
  88. </html>
复制代码
   index.jsp

  1. <%--
  2.   Created by IntelliJ IDEA.
  3.   User: NIL
  4.   Date: 2024/7/17
  5.   Time: 9:10
  6.   To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10.   <head>
  11.     <title>$Title$</title>
  12.   </head>
  13.   <body>
  14.   $END$
  15.   </body>
  16. </html>
复制代码
   shopping.jsp

  1. <%@ page import="java.util.Date" %>
  2. <%@ page import="java.text.SimpleDateFormat" %>
  3. <%@ page import="entiy.Product" %>
  4. <%@ page import="java.util.List" %>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"
  6.          pageEncoding="UTF-8"%>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10.     <title>购物车</title>
  11.     <meta charset="utf-8"/>
  12.     <style type="text/css">
  13.         body {
  14.             background-color: #f0f0f0; /* 设置整个页面的背景颜色 */
  15.             background-image: url('img/f2.png'); /* 设置背景图片 */
  16.             background-size: cover; /* 图片铺满整个页面 */
  17.             background-repeat: no-repeat; /* 不重复显示背景图片 */
  18.             background-attachment: fixed; /* 固定背景图片,不随页面滚动 */
  19.             font-family: Arial, sans-serif; /* 设置页面字体 */
  20.             margin: 0; /* 去除页面默认的边距 */
  21.             padding: 0; /* 去除页面默认的内边距 */
  22.         }
  23.         h1 {
  24.             text-align: center;
  25.             margin-top: 20px;
  26.             color: #555;
  27.         }
  28.         .container {
  29.             max-width: 1200px;
  30.             margin: 0 auto;
  31.             padding: 20px;
  32.         }
  33.         .user-info {
  34.             text-align: right;
  35.             margin-top: 10px;
  36.             margin-right: 10px;
  37.         }
  38.         table {
  39.             width: 100%;
  40.             border: 1px solid #ddd;
  41.             border-collapse: collapse;
  42.             background-color: #fff;
  43.             margin-top: 20px;
  44.         }
  45.         table th, table td {
  46.             border: 1px solid #ddd;
  47.             padding: 12px;
  48.             text-align: center;
  49.         }
  50.         th {
  51.             background-color: #f2f2f2;
  52.         }
  53.         td img {
  54.             max-width: 50px;
  55.             display: block;
  56.             margin: 0 auto;
  57.         }
  58.         .button {
  59.             padding: 12px 24px;
  60.             font-size: 16px;
  61.             background-color: #007bff;
  62.             color: #fff;
  63.             border: none;
  64.             cursor: pointer;
  65.             transition: background-color 0.3s;
  66.         }
  67.         .button:hover {
  68.             background-color: #0056b3;
  69.         }
  70.         .checkout {
  71.             text-align: center;
  72.             margin-top: 20px;
  73.         }
  74.         input[type="submit"] {
  75.             padding: 14px 28px;
  76.             font-size: 18px;
  77.             background-color: #28a745;
  78.             color: #fff;
  79.             border: none;
  80.             cursor: pointer;
  81.             transition: background-color 0.3s;
  82.         }
  83.         input[type="submit"]:hover {
  84.             background-color: #218838;
  85.         }
  86.     </style>
  87. </head>
  88. <body>
  89. <h1>交易平台</h1>
  90. <!-- 用户信息 -->
  91. <div style="text-align: right; margin-top: 10px; margin-right: 10px;">
  92.     <form action="Shopping" method="post">
  93.         <input type="hidden" name="action" value="getUsername">
  94.         <%
  95.             String username = (String) session.getAttribute("username");
  96.             if (username == null) {
  97.                 username = "游客";
  98.             }
  99.         %>
  100.         <span>您好,<%= session.getAttribute("username") %></span>
  101.         <span> | 现在时间:  <%
  102.             Date date = new Date();
  103.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  104.         %>
  105.         <%=sdf.format(date )%>
  106.         </span>
  107.     </form>
  108. </div>
  109. <table>
  110.     <tr>
  111.         <th>商品</th>
  112.         <th>单价(元)</th>
  113.         <th>操作</th>
  114.     </tr>
  115.     <%
  116.         List<Product> products = (List<Product>) request.getAttribute("goods");
  117.         if (products == null) {
  118.             System.out.println("Products list is null!");
  119.             // 可以根据实际情况做进一步处理,如返回到上一页或者显示错误信息。
  120.         } else {
  121.             for (int i=0; i<products.size(); i++) {
  122.                 Product e = products.get(i);
  123.     %>
  124.     <tr>
  125.         <td><%=e.getName()%></td>
  126.         <td><%=e.getPrice()%></td>
  127.         <td>
  128.             <a href="load?id=<%=e.getId()%>">修改</a>
  129.             <input type="button" value="加入购物车" onclick="add_shoppingcart(this);">
  130.         </td>
  131.     </tr>
  132.     <%
  133.             }
  134.         }
  135.     %>
  136. </table>
  137. <p>
  138.     <input type="button" class="button" value="添加商品" onclick="location='AddShopping.jsp'"/>
  139. </p>
  140. <h1>购物车</h1>
  141. <form action="total" method="post">
  142. <table>
  143.     <thead>
  144.     <tr>
  145.         <th>商品</th>
  146.         <th>单价(元)</th>
  147.         <th>数量</th>
  148.         <th>金额(元)</th>
  149.         <th>删除</th>
  150.     </tr>
  151.     </thead>
  152.     <tbody id="goods">
  153.     </tbody>
  154.     <tfoot>
  155.     <tr>
  156.         <td colspan="3" align="right">总计</td>
  157.         <td><input type="text" name="total" id="total" value="" /> </td>
  158.         <td></td>
  159.     </tr>
  160.     </tfoot>
  161. </table>
  162. <!-- 去支付按钮 -->
  163. <div style="text-align: center; margin-top: 20px;">
  164.         <input type="submit" value="去支付" onclick="f1();" />
  165. </div>
  166.     </form>
  167. <p>
  168.    <input type="button"value="历史清单" onclick="location='LSQD'"/>
  169. </p>
  170. <script type="text/javascript">
  171.    function f1(){
  172.        alert("支付成功!");
  173.    }
  174.     function del(btn){
  175.         //获取当前点击的删除按钮所在的tr
  176.         var tr = btn.parentNode.parentNode;
  177.         tr.parentNode.removeChild(tr);
  178.         var tds = tr.getElementsByTagName("td");
  179.         total();
  180.     }
  181.     function add(btn){
  182.         var td = btn.parentNode;
  183.         var inputs = td.getElementsByTagName("input");
  184.         var text = inputs[1];
  185.         var amount = parseInt(text.value)+1;
  186.         text.value = amount;
  187.         var tr = btn.parentNode.parentNode;
  188.         var tds = tr.getElementsByTagName("td");
  189.         var pr = parseFloat(tds[1].innerText);
  190.         mny1 = pr*amount;
  191.         //alert(mny1)
  192.         tds[3].innerText = mny1;
  193.         total();
  194.     }
  195.     function minu(btn){
  196.         var td = btn.parentNode;
  197.         var tr = td.parentNode;
  198.         var inputs = td.getElementsByTagName("input");
  199.         var text = inputs[1];
  200.         if (text.value>1){
  201.             var amount = parseInt(text.value)-1;
  202.             text.value = amount;
  203.             var tds = tr.getElementsByTagName("td");
  204.             var pr = parseInt(tds[1].innerText);
  205.             mny2 = pr*amount;
  206.             tds[3].innerText = mny2;
  207.         }else{
  208.             tr.parentNode.removeChild(tr);
  209.         }
  210.         total();
  211.     }
  212.     function total(){
  213.         //1.获取购物车中所有的商品行
  214.         var tbody = document.getElementById("goods");
  215.         var trs = tbody.getElementsByTagName("tr");
  216.         //2.遍历这些行,获取每一行的金额
  217.         var sum = 0;
  218.         for(var i=0;i<trs.length;i++){
  219.             var tr = trs[i];
  220.             //取当前行的金额
  221.             var tds = tr.getElementsByTagName("td");
  222.             var mny = parseFloat(tds[3].innerText);
  223.             sum = sum+mny;
  224.         }
  225.         document.getElementById("total").value=sum;
  226.     }
  227.     function add_shoppingcart(btn){
  228.         var tbody = document.getElementById("goods");
  229.         var tr = btn.parentNode.parentNode;
  230.         var tds = tr.getElementsByTagName("td");
  231.         var trs = tbody.getElementsByTagName("tr")
  232.         var name = tds[0].innerText;
  233.         var price = tds[1].innerText;
  234.         for (var i=0;i<trs.length;i++) {
  235.             var tr1 = trs[i];
  236.             var tds1 = tr1.getElementsByTagName("td");
  237.             var name1 = tds1[0].innerText;
  238.         }
  239.         if(name==name1) {
  240.             var TD = tds1[2];
  241.             var inputs = TD.getElementsByTagName("input");
  242.             var text = inputs[1];
  243.             var amount = parseInt(text.value) + 1;
  244.             mny1 = pr * amount;
  245.             tds1[3].innerText = mny1;
  246.             total();
  247.         }
  248.         else {
  249.             var ntr = tbody.insertRow();
  250.             ntr.innerHTML =
  251.                 '<td>'+name+'</td>'+
  252.                 '<td>'+price+'</td>'+
  253.                 '<td align="center">'+
  254.                 '<input  type="button" value="-" onclick="minu(this);"/>'+
  255.                 '<input   type="text" size="3" readonly value="1"/>'+
  256.                 '<input  type="button" value="+" onclick="add(this);"/>'+
  257.                 '</td>'+
  258.                 '<td>'+price+'</td>'+
  259.                 '<td align="center"><input type="button" value="x" onclick="del(this);"/></td></tr>';
  260.             total();}
  261.     }
  262. </script>
  263. </body>
  264. </html>
复制代码
  Update.jsp

  1. <%@ page import="entiy.Product" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6.     <title>修改商品信息</title>
  7.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8.     <link rel="stylesheet" type="text/css"
  9.           href="css/style.css" />
  10. </head>
  11. <body>
  12. <div id="wrap">
  13.     <div id="top_content">
  14.         <div id="header">
  15.             <div id="rightheader">
  16.             </div>
  17.             <div id="topheader">
  18.                 <h1 id="title">
  19.                    商品修改
  20.                 </h1>
  21.             </div>
  22.             <div id="navigation">
  23.             </div>
  24.         </div>
  25.         <div id="content">
  26.             <p id="whereami">
  27.             </p>
  28.             <h1>
  29.                 修改商品信息:
  30.             </h1>
  31.             <form action="update" method="post">
  32.                 <%
  33.                     Product product = (Product) request.getAttribute("goods");
  34.                     System.out.println(product.getId());
  35.                 %>
  36.                 <table cellpadding="0" cellspacing="0" border="0"
  37.                        class="form_table">
  38.                     <tr>
  39.                         <td valign="middle" align="right">
  40.                             商品:
  41.                         </td>
  42.                         <td valign="middle" align="left">
  43.                             <input type="text" class="inputgri" name="name" value="<%=product.getName()%>" />
  44.                         </td>
  45.                     </tr>
  46.                     <tr>
  47.                         <td valign="middle" align="right">
  48.                             价格:
  49.                         </td>
  50.                         <td valign="middle" align="left">
  51.                             <input type="text" class="inputgri" name="price" value="<%=product.getPrice()%>" />
  52.                             <input type="hidden" name="id" value="<%=product.getId()%>">
  53.                         </td>
  54.                     </tr>
  55.                 </table>
  56.                 <p>
  57.                     <input type="submit" class="button" value="提交" />
  58.                 </p>
  59.             </form>
  60.         </div>
  61.     </div>
  62.     <div id="footer">
  63.         <div id="footer_bg">
  64.             ABC@126.com
  65.         </div>
  66.     </div>
  67. </div>
  68. </body>
  69. </html>
复制代码
  zhuce.jsp

  1. <%@page contentType="text/html;charset=utf-8" pageEncoding="UTF-8" %>
  2. <%@page import="java.util.*,java.text.*,entiy.*" %>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6.     <title>交易网注册</title>
  7.     <script>
  8.         function f2(){
  9.             alert("确认注册?");
  10.         }
  11.     </script>
  12.     <style>
  13.         h1 {
  14.             font-size: 40px;
  15.             color: blanchedalmond;
  16.             text-align: center;
  17.             font-family: 'Courier New', Courier, monospace;
  18.             font-style: italic;
  19.         }
  20.     </style>
  21. </head>
  22. <body>
  23. <h1 name="top">交易网</h1>
  24. <table align="center" cellspacing="0">
  25.     <tr>
  26.         <td>
  27.             <table border="1" cellspacing="0">
  28.                 <form action="zhuce" method="post">
  29.                     <tr>
  30.                         <td>
  31.                             <table cellpadding="30">
  32.                                 <tr>
  33.                                     <td colspan="2" align="center"><strong>注册</strong></td>
  34.                                 </tr>
  35.                                 <tr>
  36.                                     <td>昵称:
  37.                                         <input type="text" id="name" name="name">
  38.                                     </td>
  39.                                 </tr>
  40.                                 <tr>
  41.                                     <td>帐号:
  42.                                         <input type="text" id="idname" name="idname">(输入九位以内整数字)
  43.                                     </td>
  44.                                 </tr>
  45.                                 <tr>
  46.                                     <td>密码:
  47.                                         <input type="password" id="pd" name="pd">
  48.                                     </td>
  49.                                 </tr>
  50.                                 <tr>
  51.                                     <td align="center" colspan="2">
  52.                                         <input type="submit" value="注册" onclick="f2();">
  53.                                     </td>
  54.                                 </tr>
  55.                             </table>
  56.                         </td>
  57.                     </tr>
  58.                 </form>
  59.             </table>
  60.         </td>
  61.     </tr>
  62.     <tr>
  63.         <td align="right">关于交易网</td>
  64.     </tr>
  65. </table>
  66. </body>
  67. </html>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用多少眼泪才能让你相信

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表