这个基于JavaWeb的在线商城项目是我们专业这学期期末实训的项目
项目视频演示
期末实训项目-JavaWeb在线商城系统(java+jsp+servlet+MySQL+jdbc+css+js+jQuery)
获取项目数据和数据库:
期末实训项目-JavaWeb在线商城系统
gitee链接:
https://gitee.com/yuhaowen/ShoppingMall.git
GitHub:
https://github.com/yuhaowen0917/JavaWeb-ShoppingMall.git
项目资源和数据库
https://download.csdn.net/download/yuyunbai0917/85839278
项目结构图
商城首页.html
实训在线商城
手机
电脑
家用电器
登录注册页面.html
在线商城注册页面
car.html
商城购物车界面
全选
图片
商品名称
商品信息
单价
数量
小计
操作
全选
已选择数量:
0
件
总价是:
¥
00.00
car.js
能够实现购物车中对商品的复选框全选、计算商品的数量小计,以及购物车中商品总价
$(function(){
// $(".c-sum_num").click(function(){
// console.info($(this).text());
// })
totl();
goodsnum();
// 全选
$(".all").click(function() {
let all = $(this).prop("checked")
$(".each").each(function() {
$(this).prop("checked", all);
})
})
// 减少商品数量
$(".reduce").click(function(){
var num = $(this).siblings(".text_num").val();
if(num>0){
num--;
$(this).siblings(".text_num").val(num);
}
var price=$(this).parents().siblings(".c-price").children(".c-price_num").text();
var sum_num = $(this).parents().siblings(".c-sum").children(".c-sum_num");
var sum = parseFloat(price*num);
// console.log(sum);
$(sum_num).text(sum);
totl();
goodsnum();
});
// 增加商品数量
$(".add").click(function(){
var num = $(this).siblings(".text_num").val();
num++;
$(this).siblings(".text_num").val(num);
var price=$(this).parents().siblings(".c-price").children(".c-price_num").text();
var sum_num = $(this).parents().siblings(".c-sum").children(".c-sum_num");
var sum = parseFloat(price*num);
// console.log(sum);
$(sum_num).text(sum);
totl();
goodsnum();
});
// 删除商品
$(".remove").click(function(){
$(this).parents(".carts-goods").remove();
totl();
goodsnum();
});
// 总价
function totl(){
let sumprice = 0;
$.each($(".c-sum_num"),function() {
sumprice+=parseFloat($(this).text());
$(".show-money span").text(sumprice);
});
}
// 统计商品数量
function goodsnum(){
let goods_num=0;
$.each($(".text_num"),function() {
goods_num+=parseInt($(this).val());
$(".goods_num span").text(goods_num);
});
}
});
user.java
package User;
public class user {
private int user_id;
private String username;
private String password;
public int getId() {
return user_id;
}
public void setId(int id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [user_id=" + user_id + ", username=" + username + ", password=" + password + "]";
}
public user(int id, String username, String password, String address) {
super();
this.user_id = user_id;
this.username = username;
}
public user() {
super();
}
}
RegisterController.java
package controller;
import shop_servlet.Register;
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 java.io.IOException;
//import service.UserService2;
@WebServlet("/register.do")
public class RegisterController extends HttpServlet{
private static final long serialVersionUID = 7804524886360637172L;
public RegisterController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String user_id = request.getParameter("user_id");
String password = request.getParameter("password");
String email = request.getParameter("email");
String birth = request.getParameter("birth");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
Register userService = new Register();
if(userService.register(username, user_id, password, email, birth, phone, address) > 0) {
response.getWriter().print("register success!");
}else {
response.getWriter().print("register false!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
LoginController.java
package controller;
import java.io.IOException;
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 shop_servlet.Register;
@WebServlet("/login.do")
public class LoginController extends HttpServlet{
private static final long serialVersionUID = 7242872008838278971L;
public LoginController() {
super();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String user_id = request.getParameter("user_id");
String password = request.getParameter("password");
Register userService = new Register();
if(userService.login(user_id,password)) {
request.getSession().setAttribute("user_id", user_id);
response.sendRedirect("first.jsp");
}else {
response.getWriter().print("register false!");
}
}catch(Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Car.java
package beans;
public class Car {
private String goods_id;
private String goods_name;
private String unitprice;
private String quantity;
private String userid;
private String pic;
public String getGoods_id() {
return goods_id;
}
public void setGoods_id(String goods_id) {
this.goods_id = goods_id;
}
public String getGoods_name() {
return goods_name;
}
public void setGoods_name(String goods_name) {
this.goods_name = goods_name;
}
public String getUnitprice() {
return unitprice;
}
public void setUnitprice(String unitprice) {
this.unitprice = unitprice;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}
AddCarController.java
package controller;
import beans.Car;
import beans.Good;
import utils.JDBCUtils;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
//import service.UserService2;
@WebServlet("/addcar.do")
public class AddCarController extends HttpServlet{
private static final long serialVersionUID = 7804524886360637172L;
public AddCarController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
HttpSession session= request.getSession();
String id = request.getParameter("id");
Object user_id= session.getAttribute("user_id");
//1查询
//查询商品
String sql="SELECT * FROM goods WHERE goods_id=?";
Connection connection= JDBCUtils.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, id);
ResultSet rs = stmt.executeQuery();
Good good=new Good();
while (rs.next()) {
good.setGoods_id(rs.getString("goods_id"));
good.setGoods_name(rs.getString("goods_name"));
good.setUnitprice(rs.getString("unitprice"));
good.setDetails(rs.getString("details"));
good.setPic(rs.getString("photo"));
}
//2添加到数据库
String sqladd="INSERT INTO car (goods_id, goods_name,unitprice,quantity,userid,pic) VALUES (?,?,?,?,?,?)";
stmt = connection.prepareStatement(sqladd);
stmt.setString(1, good.getGoods_id());
stmt.setString(2,good.getGoods_name());
stmt.setString(3,good.getUnitprice());
stmt.setString(4,"1");
stmt.setString(5,user_id.toString());
stmt.setString(6, good.getPic());
int res=stmt.executeUpdate();//执行sql语句
if (res>0){
System.out.println("插入成功");
}
//3返回购物车
String carsql="select * from car where userid=?";
stmt = connection.prepareStatement(carsql);
stmt.setString(1, user_id.toString());
//查询商品
rs = stmt.executeQuery();
Car car=null;
List carList=new ArrayList<>();
while (rs.next()) {
car=new Car();
car.setGoods_id(rs.getString("goods_id"));
car.setGoods_name(rs.getString("goods_name"));
car.setUnitprice(rs.getString("unitprice"));
car.setQuantity(rs.getString("quantity"));
car.setPic(rs.getString("pic"));
carList.add(car);
}
connection.close();
request.getSession().setAttribute("carList", carList);
response.sendRedirect("car.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Goods.java
package beans;
public class Good {
private String goods_id;
private String goods_name;
private String unitprice;
private String details;
private String pic;
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getGoods_id() {
return goods_id;
}
public void setGoods_id(String goods_id) {
this.goods_id = goods_id;
}
public String getGoods_name() {
return goods_name;
}
public void setGoods_name(String goods_name) {
this.goods_name = goods_name;
}
public String getUnitprice() {
return unitprice;
}
public void setUnitprice(String unitprice) {
this.unitprice = unitprice;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
@Override
public String toString() {
return "User [goods_id=" + goods_id + ", goods_name=" + goods_name + ", unitprice=" + unitprice + ", details=" + details + "]";
}
public Good(String goods_id, String goods_name, String unitprice, String details) {
super();
this.goods_id = goods_id;
this.goods_name = goods_name;
this.unitprice = unitprice;
this.details = details;
}
public Good() {
super();
}
}
DetailController.java
package controller;
import beans.Good;
import utils.JDBCUtils;
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 java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import service.UserService2;
@WebServlet("/detail.do")
public class DetailController extends HttpServlet{
private static final long serialVersionUID = 7804524886360637172L;
public DetailController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String id = request.getParameter("id");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//查询商品
String sql="SELECT * FROM goods WHERE goods_id=?";
Connection connection= JDBCUtils.getConnection();
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, id);
ResultSet rs = stmt.executeQuery();
Good good=new Good();
while (rs.next()) {
good.setGoods_id(rs.getString("goods_id"));
good.setGoods_name(rs.getString("goods_name"));
good.setUnitprice(rs.getString("unitprice"));
good.setDetails(rs.getString("details"));
good.setPic(rs.getString("photo"));
}
connection.close();
request.getSession().setAttribute("good", good);
response.sendRedirect("phone.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
内容太多,文章里面只能够简单的描述一下,我正在整理文件,后面会将项目的所有文件打包上传到Gitee上面,供大家下载下来学习使用
这个项目我做得并不是很完美,各位可以发挥自己的才能去完善它
码字不易,还请点赞支持一下,谢谢