【软件应用开发】jsp+servlet实现网上书城(简易版)

jsp+servlet实现网上书城(简易版)

编程语言:Java(jdk 1.8)
开发IDE:eclipse
数据库:mysql 8.0
使用Tomcat部署工程后访问:http://localhost:8080/BooksCart
工程目录结构:
【软件应用开发】jsp+servlet实现网上书城(简易版)_第1张图片

booklist.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>图书列表title>
    <link href="css/layout.css" rel="stylesheet" type="text/css"/>
    <link href="css/list.css" rel="stylesheet" type="text/css"/>
head>
<body>
<table class="tablelist">
    <form action="AddServlet" method="post">
        <thead>
        <tr>
            <th><input type="checkbox" id="check"/>全选th>
            <th>编号th>
            <th>封面th>
            <th>图书名称th>
            <th>单价(元)th>
            <th>状态th>
        tr>
        thead>
        <tbody>
        <c:forEach items="${books}" var="book" varStatus="s">
            <c:set var="t" value="0">c:set>
            <c:forEach items="${cart}" var="cart">
                <c:if test="${cart.id==book.id}">
                    <c:set var="t" value="1">c:set>
                c:if>
            c:forEach>
            <tr>
                 
                <c:if test="${t==0}">
                    <td><input type="checkbox" name="checkbox" value="${book.id}"/>td>
                c:if>
                
                <c:if test="${t!=0}">
                    <td>td>
                c:if>
                <td>${s.count}td>
                <td class="imgtd"><img src="${book.image}" height="110" width="78"/>td>
                <td>${book.name}td>
                <td>${book.price}元/本td>
                <td>
                        
                    <c:if test="${t==0}">未添加到购物车c:if>
                        
                    <c:if test="${t!=0}"><font color="blue">已添加font>c:if>
                td>
            tr>
        c:forEach>

        tbody>
        <thead>
        <tr>
            <th>th>
            <th>th>
            <th>th>
            <th>th>
            <th>th>
            <th><input type="submit" style="background-color:green" value="添加购物车"/>th>
        tr>
        thead>
    form>
table>
body>

<script>
    document.getElementById("check").onclick = function () {
        var checked = document.getElementById("check").checked;
        var checkson = document.getElementsByName("checkbox");
        if (checked) {
            for (var i = 0; i < checkson.length; i++) {
                checkson[i].checked = true;
            }
        } else {
            for (var i = 0; i < checkson.length; i++) {
                checkson[i].checked = false;
            }
        }
    }
script>
html>

1.首页图书列表

【软件应用开发】jsp+servlet实现网上书城(简易版)_第2张图片

cart.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>图书列表title>
    <link href="css/layout.css" rel="stylesheet" type="text/css"/>
    <link href="css/list.css" rel="stylesheet" type="text/css"/>

head>
<body>
<table class="tablelist">

    <thead>
    <tr>
        <th>编号th>
        <th>封面th>
        <th>图书名称th>
        <th>单价(元)th>
        <th>数量(件)th>
        <th>小计(元)th>
        <th>操作th>
    tr>
    thead>
    <tbody>
    <c:forEach items="${cart}" var="cart" varStatus="s">
        <tr>
            <td>${s.count}td>
            <td class="imgtd"><img src="${cart.image}" height="110" width="78"/>td>
            <td>${cart.name}td>
            <td>${cart.price}元td>
            <td>
                <form action="CountServlet" method="post">
                    <input type="hidden" name="cartid" value="${cart.id}"/>
                     <button type="submit" name="button" value="-"style="width: 30px">-button>
                    <input type=text style="width: 50px" name=amount value=${cart.number}>
                    <button type="submit" name="button" value="+" style="width: 30px">+button>
                form>
            td>
            <td>${cart.totalprice}元td>
            <td><a href="RemoveServlet?id=${cart.id}" type="button" class="tablelink"><img src="images/remove.jpg" width="20" height="14"/>移出购物车a>td>
        tr>
    c:forEach>
    tbody>
    <thead>
    <tr>
        <th>th>
        <th>th>
        <th>th>
        <th>th>
        <th>共选${sumnumber}件th>
        <th>总价${sumprice}元th>
        <th><a href="PayServlet" type="button" class="tablelink"><img src="images/jiesuan.jpg" width="20" height="20"/>结算a>th>
    tr>
    thead>
table>
body>
html>

2.选择图书添加至购物车

【软件应用开发】jsp+servlet实现网上书城(简易版)_第3张图片
PayServlet.java

package com.servlet;

import com.dao.BooksUtilsDao;

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;


@WebServlet("/PayServlet")
public class PayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("cart");
        BooksUtilsDao dao = new BooksUtilsDao();
        try {
            dao.deletecart();
        } catch (Exception e) {
            e.printStackTrace();
        }
        double sumprice = (Double) request.getSession().getAttribute("sumprice");
        response.getWriter().println("结算成功, 共花费:"+sumprice+"元!");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

3.购物车结算

【软件应用开发】jsp+servlet实现网上书城(简易版)_第4张图片
【软件应用开发】jsp+servlet实现网上书城(简易版)_第5张图片
BooksUtilsDao.java

package com.dao;

import com.bean.Book;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.utils.DBCP;

import java.util.ArrayList;
import java.util.List;


public class BooksUtilsDao {
    public static QueryRunner runner = new QueryRunner(DBCP.getDataSource());
    //查询书表
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Book> findAll() throws Exception{
        ArrayList<Book> list = new ArrayList<Book>();
        String sql = "select * from books";
        list = (ArrayList<Book>) runner.query(sql, new BeanListHandler(Book.class));
        return list;
    }
    //根据id查询书的信息
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Book> findId(String id) throws Exception{
        ArrayList<Book> list = new ArrayList<Book>();
        String sql = "select * from books where id = ?";
        list = (ArrayList<Book>) runner.query(sql, new BeanListHandler(Book.class), id);
        return list;
    }

    //根据id增加书的数量
    public void add(String id) throws Exception{
        String sql = "update books set number = number+'1' where id = ?";
        runner.update(sql, new Object[]{id});
    }

    //根据id减少书的数量
    public void reduce(String id) throws Exception{
        String sql = "update books set number = number-'1' where id = ?";
        runner.update(sql, new Object[]{id});

    }

    //清空购物车
    public void remove(String id) throws Exception{
        String sql = "update books set number = 0, totalprice = 0 where id = ?";
        runner.update(sql, new Object[]{id});

    }
    //计算购物车单本图书的总价格(小计)
    public void totalprice(String id) throws Exception{
        String sql = "update books set totalprice = price*number where id = ?";
        runner.update(sql, new Object[]{id});

    }

    //计算购物车总价格
    public double sumprice() throws Exception{
        String sql = "select sum(totalprice) from books ";
        @SuppressWarnings({ "unchecked", "rawtypes" })
		double sumprice = runner.query(sql, new ScalarHandler());
        return sumprice;
    }

    //计算购物车总数量
    public double sumnumber() throws Exception{
        String sql = "select sum(number) from books ";
        @SuppressWarnings({ "unchecked", "rawtypes" })
		double sumnumber = runner.query(sql, new ScalarHandler());
        return sumnumber;
    }

    //将数据库购物车信息删除
    public void deletecart() throws Exception{
        String sql = "update books set number = 0, totalprice = 0";
        runner.update(sql);
    }

}

需要完整代码可参考:https://download.csdn.net/download/weixin_47936614/85474339

你可能感兴趣的:(软件应用开发,java,tomcat,eclipse)