JavaWeb实战之开发网上购物系统(超详细)

时间:2022-03-23 01:54:06

一、项目描述

基于B/S三层架构设计的购物系统,可以更好地符合"高内聚,低耦合"的设计思想。表现层(UI)通过JSP在浏览器上实现界面展示,通过Servlet接受用户请求并生成验证码等实时信息;数据访问层(DAL)通过JDBC对DBMS进行访问;二者之间的业务逻辑层(BLL)通过Service进行连接。同时面向接口编程提高规范化。这只是我自己用两三天的时间做的小项目,距离真正的购物系统差距还是很大的,仅供初学者参考,很多逻辑缺陷不必太纠结,以后有时间会再写一篇博客对这个项目进行相关测试。

如果只需要代码可直接通过书签栏跳转至相关文件。

二、项目演示

这是购物系统首页,是仿照华为商城设计的,当然也可以说是仿照小米商城,这二者的页面设计并无本质区别,具体实现借助于Bootstrap,商品信息通过后台数据库读取,商品表见下

JavaWeb实战之开发网上购物系统(超详细)
JavaWeb实战之开发网上购物系统(超详细)

点击任意商品进入详情界面

JavaWeb实战之开发网上购物系统(超详细)

如果此时点击加入购物车会调用Servlet中的doPost方法,进而通过登陆信息是否读取成功判断登陆状态,如果未登录则转至登录界面并提示相关信息,背景渐变色借助于webgradients选取,同时实现正则验证与验证码相关功能

JavaWeb实战之开发网上购物系统(超详细)

我们先注册再登录,这里注册洛的相关信息

JavaWeb实战之开发网上购物系统(超详细)

注册成功

JavaWeb实战之开发网上购物系统(超详细)
JavaWeb实战之开发网上购物系统(超详细)

之后我们登录并将心仪的商品加入购物车,查看购物车,这里会一致显示当前登录用户信息

JavaWeb实战之开发网上购物系统(超详细)

提交订单后查看订单,这里借助jQuery做了一个下拉框,可以查看后台数据库关于此订单的全部信息,这里提交订单相当于付款,所以未设退货功能,因此数据删除只能借助于后台数据库。

JavaWeb实战之开发网上购物系统(超详细)

最后点击注销返回主页

JavaWeb实战之开发网上购物系统(超详细)

三、部分代码

JdbcUtils.java

用于加载驱动并获得与数据库的连接的工具类

  1. package common;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class JdbcUtil {
  10. static Connection connection;
  11. public static Connection getCon() {
  12. //加载驱动
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. } catch (ClassNotFoundException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. String url = "jdbc:mysql://localhost:3306/shoppingsystem?characterEncoding=utf-8";
  20. String username = "root";
  21. String password = "123";
  22.  
  23. //获得连接
  24. try {
  25. connection = DriverManager.getConnection(url, username, password);
  26. } catch (SQLException e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. }
  30.  
  31. return connection;
  32. }
  33. public static void closeAll(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
  34. if (resultSet != null) {
  35. try {
  36. resultSet.close();
  37. } catch (SQLException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. }
  42. if (preparedStatement != null) {
  43. try {
  44. preparedStatement.close();
  45. } catch (SQLException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. }
  50. if (connection != null) {
  51. try {
  52. connection.close();
  53. } catch (SQLException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }

People.java

与数据库中用户表相匹配的用户类

  1. package entity;
  2.  
  3. public class People {
  4. private String id;
  5. private String password;
  6. private String name;
  7. private String email;
  8. private String address;
  9.  
  10. public People() {
  11. super();
  12. // TODO Auto-generated constructor stub
  13. }
  14.  
  15. public People(String id, String password, String name, String email, String address) {
  16. super();
  17. this.id = id;
  18. this.password = password;
  19. this.name = name;
  20. this.email = email;
  21. this.address = address;
  22. }
  23.  
  24. public String getId() {
  25. return id;
  26. }
  27.  
  28. public void setId(String id) {
  29. this.id = id;
  30. }
  31.  
  32. public String getPassword() {
  33. return password;
  34. }
  35.  
  36. public void setPassword(String password) {
  37. this.password = password;
  38. }
  39.  
  40. public String getName() {
  41. return name;
  42. }
  43.  
  44. public void setName(String name) {
  45. this.name = name;
  46. }
  47.  
  48. public String getEmail() {
  49. return email;
  50. }
  51.  
  52. public void setEmail(String email) {
  53. this.email = email;
  54. }
  55.  
  56. public String getAddress() {
  57. return address;
  58. }
  59.  
  60. public void setAddress(String address) {
  61. this.address = address;
  62. }
  63.  
  64. @Override
  65. public String toString() {
  66. return "People [id=" + id + ", password=" + password + ", name=" + name + ", email=" + email + ", address="
  67. + address + "]";
  68. }
  69.  
  70. }

InterfacePeopleDao.java

对用户信息进行相关操作的接口

  1. package dao;
  2.  
  3. import java.util.List;
  4.  
  5. import entity.People;
  6.  
  7. public interface InterfacePeopleDao {
  8. public abstract int insertPeople(String id, String password, String name, String email, String address);
  9.  
  10. public abstract int deletePeople(String id);
  11.  
  12. public abstract int updatePeople(String id, String password, String name, String email, String address);
  13.  
  14. public abstract People checkPeople(String id, String password);
  15.  
  16. public abstract List<People> findAllPeople();
  17. }

PeopleDao.java

对上述接口的实现

  1. package dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.List;
  8.  
  9. import common.JdbcUtil;
  10. import entity.People;
  11.  
  12. public class PeopleDao implements InterfacePeopleDao {
  13. private Connection connection;
  14. private PreparedStatement preparedStatement;
  15. private ResultSet resultSet;
  16.  
  17. @Override
  18. public int insertPeople(String id, String password, String name, String email, String address) {
  19. String sql = "mistake";
  20. if (id != "" && password != "" && name != "" && email != "" && address != "") {
  21. sql = "insert into people (id, password, name, email, address)" + "values('" + id + "','" +
  22. password + "','" + name + "','" + email + "','" + address + "');";
  23. }
  24. System.out.println("insertPeople()的sql语句:" + sql);
  25.  
  26. int result = 0;
  27. connection = JdbcUtil.getCon();
  28. try {
  29. preparedStatement = connection.prepareStatement(sql);
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33. try {
  34. result = preparedStatement.executeUpdate();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. JdbcUtil.closeAll(connection, preparedStatement, resultSet);
  39. return result;
  40. }
  41.  
  42. @Override
  43. public int deletePeople(String id) {
  44. // TODO Auto-generated method stub
  45. return 0;
  46. }
  47.  
  48. @Override
  49. public int updatePeople(String id, String password, String name, String email, String address) {
  50. // TODO Auto-generated method stub
  51. return 0;
  52. }
  53.  
  54. @Override
  55. public People checkPeople(String id, String password) {
  56. String sql = null;
  57. if (id != null && password != null) {
  58. sql = "select * from people where id = '" + id + "' and password = '" + password + "'";
  59. }
  60. System.out.println("checkPeople()的sql语句:" + sql);
  61.  
  62. People people = null;
  63. connection = JdbcUtil.getCon();
  64. try {
  65. preparedStatement = connection.prepareStatement(sql);
  66. resultSet = preparedStatement.executeQuery();
  67. if (resultSet.next()) {
  68. people = new People();
  69. people.setId(resultSet.getString(1));
  70. people.setPassword(resultSet.getString(2));
  71. people.setName(resultSet.getString(3));
  72. people.setEmail(resultSet.getString(4));
  73. people.setAddress(resultSet.getString(5));
  74. }
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. } finally {
  78. JdbcUtil.closeAll(connection, preparedStatement, resultSet);
  79. }
  80. return people;
  81. }
  82.  
  83. @Override
  84. public List<People> findAllPeople() {
  85. // TODO Auto-generated method stub
  86. return null;
  87. }
  88.  
  89. }

InterfacPeopleService.java

在业务逻辑层中将表现层与数据访问层进行连接,由于项目本身并不复杂,所以只是简单实现了方法传递

  1. package service;
  2.  
  3. import java.util.List;
  4.  
  5. import entity.People;
  6.  
  7. public interface InterfacePeopleService {
  8. public abstract int insertPeople(String id, String password, String name, String email, String address);
  9.  
  10. public abstract int deletePeople(String id);
  11.  
  12. public abstract int updatePeople(String id, String password, String name, String email, String address);
  13.  
  14. public abstract People checkPeople(String id, String password);
  15.  
  16. public abstract List<People> findAllPeople();
  17. }

PeopleService.java

对上述接口的实现

  1. package service;
  2.  
  3. import java.util.List;
  4.  
  5. import dao.InterfacePeopleDao;
  6. import dao.PeopleDao;
  7. import entity.People;
  8.  
  9. public class PeopleService implements InterfacePeopleService {
  10. private InterfacePeopleDao peopleDao;
  11.  
  12. public PeopleService() {
  13. peopleDao = new PeopleDao();
  14. }
  15.  
  16. @Override
  17. public int insertPeople(String id, String password, String name, String email, String address) {
  18. return peopleDao.insertPeople(id, password, name, email, address);
  19. }
  20.  
  21. @Override
  22. public int deletePeople(String id) {
  23. // TODO Auto-generated method stub
  24. return 0;
  25. }
  26.  
  27. @Override
  28. public int updatePeople(String id, String password, String name, String email, String address) {
  29. // TODO Auto-generated method stub
  30. return 0;
  31. }
  32.  
  33. @Override
  34. public People checkPeople(String id, String password) {
  35. return peopleDao.checkPeople(id, password);
  36. }
  37.  
  38. @Override
  39. public List<People> findAllPeople() {
  40. // TODO Auto-generated method stub
  41. return null;
  42. }
  43.  
  44. }

CheckCode.java

在内存中生成验证码图片并输出到客户端

  1. package servlet;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferedImage;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.IOException;
  9.  
  10. import javax.imageio.ImageIO;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.ServletOutputStream;
  13. import javax.servlet.annotation.WebServlet;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import javax.servlet.http.HttpSession;
  18.  
  19. @WebServlet("/CheckCode")
  20. public class CheckCode extends HttpServlet {
  21. private static final long serialVersionUID = 1L;
  22. private static int width=70;
  23. private static int height=25;
  24.  
  25. public CheckCode() {
  26. super();
  27. // TODO Auto-generated constructor stub
  28. }
  29.  
  30. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  31. /**
  32. * request是客户端浏览器发出的请求被封装形成的一个HttpServletRequest公共接口类对象
  33. * session对象存储特定用户会话所需的属性及配置信息, 这样当用户在应用程序的Web页之间跳转时, 存储在session对象中的变量将不会丢失, 而是在整个用户会话中一直存在下去
  34. * getSession()与getSession(true)一样, 获取request对象关联的session对象, 如果没有session, 则返回一个新的session
  35. */
  36. HttpSession session=request.getSession();
  37. response.setContentType("image/jpeg");// 设置生成的文档类型为jpg
  38. ServletOutputStream servletOutputStream = response.getOutputStream();
  39.  
  40. /*设置浏览器不要缓存此图片*/
  41. response.setHeader("Pragma", "No-cache");
  42. response.setHeader("Cache-Control", "no-cache");
  43. response.setDateHeader("Expires", 0);
  44.  
  45. /**
  46. * 创建内存图片并获得其图形上下文
  47. * Image是一个抽象类, BufferedImage是其实现类, 是一个带缓冲区图像类, 主要作用是将一幅图片加载到内存中
  48. * BufferedImage生成的图片在内存里有一个图像缓冲区, 利用这个缓冲区我们可以很方便地操作这个图片
  49. */
  50. BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  51. Graphics graphics=image.getGraphics();
  52.  
  53. char[] rands=generCode();// 产生随机的验证码
  54. drawRands(graphics, rands);// 产生图像
  55. graphics.dispose();// 结束图像的绘制过程,完成图像
  56.  
  57. /*将图像输出到客户端*/
  58. ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
  59. ImageIO.write(image, "jpeg", byteArrayOutputStream);
  60. byte[] buf=byteArrayOutputStream.toByteArray();
  61. response.setContentLength(buf.length);
  62. servletOutputStream.write(buf);
  63. byteArrayOutputStream.close();
  64. servletOutputStream.close();
  65.  
  66. session.setAttribute("checkcode", new String(rands));// 将当前的验证码写入到session中
  67.  
  68. }
  69.  
  70. private char[] generCode() {
  71. //定义验证码的字符表
  72. String chars="0123456789";
  73. char[] rands=new char[5];
  74. for(int i=0;i<5;i++) {
  75. int rand=(int)(Math.random()*10);
  76. rands[i]=chars.charAt(rand);
  77. }
  78. return rands;
  79. }
  80.  
  81. private void drawRands(Graphics g,char[] rands) {
  82. g.setColor(Color.WHITE);
  83. g.setFont(new Font(null, Font.ITALIC|Font.BOLD, 18));
  84.  
  85. /*在不同高度上输出验证码的每个字符*/
  86. g.drawString("" + rands[0], 0, 18);
  87. g.drawString("" + rands[1], 14, 18);
  88. g.drawString("" + rands[2], 28, 18);
  89. g.drawString("" + rands[3], 42, 18);
  90. g.drawString("" + rands[4], 56, 18);
  91. System.out.print("当前验证码:");
  92. System.out.println(rands);
  93. }
  94.  
  95. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  96. doGet(request, response);
  97. }
  98.  
  99. }

到此这篇关于Java实战之利用JavaWeb开发购物系统(超详细)的文章就介绍到这了,更多相关JavaWeb购物系统内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_43686863/article/details/115861940