基于JSP+Servlet+MySQL的网上书店系统

登录页面

用户注册

图书添加

图书信息管理

添加图书图片

用户信息管理

技术描述

开发工具: Idea/Eclipse
数据库: mysql
Jar包仓库: Jar包
前段框架:jquery/Jsp
后端框架: Jsp/Servlet

package com.tenton.servlet;

import com.tenton.pojo.Book;
import com.tenton.service.BookService;
import com.tenton.service.Impl.BookServiceImpl;
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.math.BigDecimal;

/**
 * @Date: 2021/1/19
 * @Author: Tenton
 * @Description: 根据用户Id删除用户
 */
@WebServlet(name = "AddBookServlet",urlPatterns = "/addBookServlet")
public class AddBookServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式,防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        BookService bookService = new BookServiceImpl();
        //图书Id
        String bookId = request.getParameter("id");
        int id = 0;
        //判断是否为空,然后再进行转化为int类型
        if(bookId !=null && !bookId.equals("")){
            id = Integer.parseInt(bookId);
        }
        //图书名称
        String bookName = request.getParameter("bookName");
        //作者
        String author = request.getParameter("author");
        //出版社
        String press = request.getParameter("press");
        //图书单价
        String bookPrice = request.getParameter("price");
        BigDecimal price = new BigDecimal(bookPrice);
        //数量
        int num = Integer.parseInt(request.getParameter("num"));
        //图书插图 路径
        String image = request.getParameter("image");
        if (id > 0) {
            //创建一个Book实体,保存前端传递过来的值
            Book book = new Book(id, bookName, author, press, price, num, image);
            int result = bookService.insertBook(book);
            //判断是否添加成功
            if (result > 0) {
                response.getWriter().print("<script>alert('添加成功!')</script>");
                response.sendRedirect("/listBookServlet");
            } else {
                response.getWriter().write("添加失败,2秒后自动跳转到主界面");
                response.setHeader("Refresh", "3;URL=/listBookServlet");
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
package com.tenton.servlet;

import com.tenton.pojo.User;
import com.tenton.service.Impl.UserServiceImpl;
import com.tenton.service.UserService;

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.util.List;

/**
 * @Date: 2021/1/19
 * @Author: Tenton
 * @Description: 查询所有用户
 */
@WebServlet(name = "ListUserServlet",urlPatterns = "/listUserServlet")
public class ListUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式,防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        UserService userService = new UserServiceImpl();
        //获取所有用户信息
        List<User> users = userService.listUser();
        //把获取到的用户信息链表存入request域中,传递给前端
        request.setAttribute("users",users);
        //请求转发到userManager页面
        request.getRequestDispatcher("/userManager.jsp").forward(request,response);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2016855150

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值