一、项目描述
基于B/S三层架构设计的购物系统,可以更好地符合"高内聚,低耦合"的设计思想。表现层(UI)通过JSP在浏览器上实现界面展示,通过Servlet接受用户请求并生成验证码等实时信息;数据访问层(DAL)通过JDBC对DBMS进行访问;二者之间的业务逻辑层(BLL)通过Service进行连接。同时面向接口编程提高规范化。这只是我自己用两三天的时间做的小项目,距离真正的购物系统差距还是很大的,仅供初学者参考,很多逻辑缺陷不必太纠结,以后有时间会再写一篇博客对这个项目进行相关测试。
如果只需要代码可直接通过书签栏跳转至相关文件。
二、项目演示
这是购物系统首页,是仿照华为商城设计的,当然也可以说是仿照小米商城,这二者的页面设计并无本质区别,具体实现借助于Bootstrap,商品信息通过后台数据库读取,商品表见下
点击任意商品进入详情界面
如果此时点击加入购物车会调用Servlet中的doPost方法,进而通过登陆信息是否读取成功判断登陆状态,如果未登录则转至登录界面并提示相关信息,背景渐变色借助于webgradients选取,同时实现正则验证与验证码相关功能
我们先注册再登录,这里注册洛的相关信息
注册成功
之后我们登录并将心仪的商品加入购物车,查看购物车,这里会一致显示当前登录用户信息
提交订单后查看订单,这里借助jQuery做了一个下拉框,可以查看后台数据库关于此订单的全部信息,这里提交订单相当于付款,所以未设退货功能,因此数据删除只能借助于后台数据库。
最后点击注销返回主页
三、部分代码
JdbcUtils.java
用于加载驱动并获得与数据库的连接的工具类
package common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcUtil {
static Connection connection;
public static Connection getCon() {
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/shoppingsystem?characterEncoding=utf-8";
String username = "root";
String password = "123";
//获得连接
try {
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static void closeAll(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
、
People.java
与数据库中用户表相匹配的用户类
package entity;
public class People {
private String id;
private String password;
private String name;
private String email;
private String address;
public People() {
super();
// TODO Auto-generated constructor stub
}
public People(String id, String password, String name, String email, String address) {
super();
this.id = id;
this.password = password;
this.name = name;
this.email = email;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "People [id=" + id + ", password=" + password + ", name=" + name + ", email=" + email + ", address="
+ address + "]";
}
}
InterfacePeopleDao.java
对用户信息进行相关操作的接口
package dao;
import java.util.List;
import entity.People;
public interface InterfacePeopleDao {
public abstract int insertPeople(String id, String password, String name, String email, String address);
public abstract int deletePeople(String id);
public abstract int updatePeople(String id, String password, String name, String email, String address);
public abstract People checkPeople(String id, String password);
public abstract List<People> findAllPeople();
}
PeopleDao.java
对上述接口的实现
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import common.JdbcUtil;
import entity.People;
public class PeopleDao implements InterfacePeopleDao {
private Connection connection;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
@Override
public int insertPeople(String id, String password, String name, String email, String address) {
String sql = "mistake";
if (id != "" && password != "" && name != "" && email != "" && address != "") {
sql = "insert into people (id, password, name, email, address)" + "values('" + id + "','" +
password + "','" + name + "','" + email + "','" + address + "');";
}
System.out.println("insertPeople()的sql语句:" + sql);
int result = 0;
connection = JdbcUtil.getCon();
try {
preparedStatement = connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
JdbcUtil.closeAll(connection, preparedStatement, resultSet);
return result;
}
@Override
public int deletePeople(String id) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int updatePeople(String id, String password, String name, String email, String address) {
// TODO Auto-generated method stub
return 0;
}
@Override
public People checkPeople(String id, String password) {
String sql = null;
if (id != null && password != null) {
sql = "select * from people where id = '" + id + "' and password = '" + password + "'";
}
System.out.println("checkPeople()的sql语句:" + sql);
People people = null;
connection = JdbcUtil.getCon();
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
people = new People();
people.setId(resultSet.getString(1));
people.setPassword(resultSet.getString(2));
people.setName(resultSet.getString(3));
people.setEmail(resultSet.getString(4));
people.setAddress(resultSet.getString(5));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.closeAll(connection, preparedStatement, resultSet);
}
return people;
}
@Override
public List<People> findAllPeople() {
// TODO Auto-generated method stub
return null;
}
}
InterfacPeopleService.java
在业务逻辑层中将表现层与数据访问层进行连接,由于项目本身并不复杂,所以只是简单实现了方法传递
package service;
import java.util.List;
import entity.People;
public interface InterfacePeopleService {
public abstract int insertPeople(String id, String password, String name, String email, String address);
public abstract int deletePeople(String id);
public abstract int updatePeople(String id, String password, String name, String email, String address);
public abstract People checkPeople(String id, String password);
public abstract List<People> findAllPeople();
}
PeopleService.java
对上述接口的实现
package service;
import java.util.List;
import dao.InterfacePeopleDao;
import dao.PeopleDao;
import entity.People;
public class PeopleService implements InterfacePeopleService {
private InterfacePeopleDao peopleDao;
public PeopleService() {
peopleDao = new PeopleDao();
}
@Override
public int insertPeople(String id, String password, String name, String email, String address) {
return peopleDao.insertPeople(id, password, name, email, address);
}
@Override
public int deletePeople(String id) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int updatePeople(String id, String password, String name, String email, String address) {
// TODO Auto-generated method stub
return 0;
}
@Override
public People checkPeople(String id, String password) {
return peopleDao.checkPeople(id, password);
}
@Override
public List<People> findAllPeople() {
// TODO Auto-generated method stub
return null;
}
}
CheckCode.java
在内存中生成验证码图片并输出到客户端
package servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/CheckCode")
public class CheckCode extends HttpServlet {
private static final long serialVersionUID = 1L;
private static int width=70;
private static int height=25;
public CheckCode() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
response.setContentType("image/jpeg");// 设置生成的文档类型为jpg
ServletOutputStream servletOutputStream = response.getOutputStream();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics=image.getGraphics();
char[] rands=generCode();// 产生随机的验证码
drawRands(graphics, rands);// 产生图像
graphics.dispose();// 结束图像的绘制过程,完成图像
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
ImageIO.write(image, "jpeg", byteArrayOutputStream);
byte[] buf=byteArrayOutputStream.toByteArray();
response.setContentLength(buf.length);
servletOutputStream.write(buf);
byteArrayOutputStream.close();
servletOutputStream.close();
session.setAttribute("checkcode", new String(rands));// 将当前的验证码写入到session中
}
private char[] generCode() {
//定义验证码的字符表
String chars="0123456789";
char[] rands=new char[5];
for(int i=0;i<5;i++) {
int rand=(int)(Math.random()*10);
rands[i]=chars.charAt(rand);
}
return rands;
}
private void drawRands(Graphics g,char[] rands) {
g.setColor(Color.WHITE);
g.setFont(new Font(null, Font.ITALIC|Font.BOLD, 18));
g.drawString("" + rands[0], 0, 18);
g.drawString("" + rands[1], 14, 18);
g.drawString("" + rands[2], 28, 18);
g.drawString("" + rands[3], 42, 18);
g.drawString("" + rands[4], 56, 18);
System.out.print("当前验证码:");
System.out.println(rands);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
到此这篇关于Java实战之利用JavaWeb开发购物系统(超详细)的文章就介绍到这了,更多相关JavaWeb购物系统内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!