1、效果图:![](https://img-blog.csdnimg.cn/direct/c7ad6c1879604999a1b17e0ca1b1d4e2.png)
2、部分代码展示
1、Login_Servlet
//<!--需要源码+wx:yi3219727434 -->
package Servlet;
import JSPMethod.Goods;
import JSPMethod.Login;
import dao.DAOFactory;
import dao.vo.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet(name = "Login_Servlet", value = "/Login_Servlet")
public class Login_Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置登录错误信息
String usererror = "";
User user = new User();
HttpSession session = request.getSession();
// 设置编码格式
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//获取当前index.jsp的绝对路径
String path = request.getContextPath();//获取当前项目的虚拟路径
// session.setAttribute("path",path);
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
session.setAttribute("path", basePath);
System.out.println("basePath:" + basePath);
System.out.println("Servlet01:" + basePath);
String id = request.getParameter("id");
String password = request.getParameter("password");
System.out.println("id--"+id);
System.out.println("password--"+password);
Login longin = new Login();
try {
int n = longin.TestByZz(id, password);
System.out.println(n);
if (n == 4) {
//存入cookie
Cookie cookie1 = new Cookie("id", id);
Cookie cookie2 = new Cookie("password", password);
cookie1.setMaxAge(20 * 3600);
cookie2.setMaxAge(20 * 3600);
response.addCookie(cookie1);
response.addCookie(cookie2);
//设置文件存放路径
//本机
//商品信息地址
String path1 = "/ClassHelpFile/Shopping/";
session.setAttribute("path1",path1);
//用户图片地址
String path2 = "/ClassHelpFile/file/";
session.setAttribute("path2",path2);
//服务器
// System.out.println("filePath"+filePath);
filePath="D:\\ClassHelpFile\\file";
//
// tempPath="F:\\internet study\\JAVAWeb\\servlet\\FinalAssignment\\untitled\\web\\ClassHelpFile\\temp";
//向主页面传name
user = DAOFactory.getIUserDAOinstance().findByid(id, password);
// System.out.println("user的姓名:"+user.getName());
//在刚登陆时获取数据库中全部商品信息
List<Commodity> commodityListAll = DAOFactory.getICommodityDAOinstance().CommodityFindAll();
session.setAttribute("commodityListAll", commodityListAll);
//将用户的信息存入session中
session.setAttribute("user", user);
//在刚登陆时获取该用户的购物车列表
List<Cats> CatsAll = DAOFactory.getICatsDAOinstance().FindAllCatsByUserid(user.getId());
session.setAttribute("CatsAll", CatsAll);
//设置userid
session.setAttribute("userid", user.getId());
//在登陆时获取用户的地址
Adress userAdress = DAOFactory.getIAdressinstance().FindAdressByUserid(user.getId());
System.out.println("userAdress");
System.out.println("userAdress----" + userAdress.getAdressname());
if (userAdress.getAdressname() != null) {
session.setAttribute("userAdress", userAdress);
}
//计算当前购物车总价
Goods goods = new Goods();
int AllPrice = goods.AllPrice(CatsAll);
session.setAttribute("AllPrice", AllPrice);
//获取当前用户的全部订单
List<Oders> OdersAll = DAOFactory.getIOdersinstance().FindCatsByUserid(user.getId());
session.setAttribute("OdersAll", OdersAll);
//计算当前用户订单中的总价
int OdersAllPrice = goods.OdersAllPrice(OdersAll);
session.setAttribute("OdersAllPrice", OdersAllPrice);
System.out.println("user的姓名:" + user.getName());
System.out.println("user的image:" + user.getUserimage());
response.sendRedirect(path + "/index_LoginSuccess.jsp");
} else if (n == 3) {
usererror = "用户名或密码错误";
session.setAttribute("usererror", usererror);
response.sendRedirect(path + "/JSP/JSP_Login/Login_Fail.jsp");
} else if (n == 2) {
usererror = "格式错误,用户id应该是0-9的9位数字且用户密码至少5-16个字符,至少1个大写字母,1个小写字母和1个数字,其他可以是任意字符";
session.setAttribute("usererror", usererror);
response.sendRedirect(path + "/JSP/JSP_Login/Login_Fail.jsp");
} else {
usererror = "用户名或密码不能为空";
session.setAttribute("usererror", usererror);
response.sendRedirect(path + "/JSP/JSP_Login/Login_Fail.jsp");
}
} catch (Exception e) {
// throw new RuntimeException(e);
response.sendRedirect("Fail.jsp");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
2、pageServlet
//<!--需要源码+wx:yi3219727434 -->
package Servlet;
import dao.DAOFactory;
import dao.vo.Commodity;
import dao.vo.PaginationHelper;
import dao.vo.User;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@WebServlet(name = "pageServlet", value = "/pageServlet")
public class pageServlet extends HttpServlet {
PaginationHelper paginationHelper = null;
@Override
public void init() throws ServletException {
System.out.println("被创建了");
paginationHelper = new PaginationHelper();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
// PaginationHelper paginationHelper = new PaginationHelper();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
PrintWriter out = response.getWriter();
User user = (User) session.getAttribute("user");
String path = (String) session.getAttribute("path");
int currentpage = 1;
try {
currentpage = Integer.valueOf(request.getParameter("currentpage"));
} catch (NumberFormatException e) {
currentpage = 1;
}
//从前台获取的数据有:currentpage 当前码页 rows:每页显示的条数
paginationHelper.setCurrentpage(currentpage);
int rows;
try {
if(request.getParameter("rows") != null){
rows = Integer.valueOf(request.getParameter("rows"));
paginationHelper.setRows(rows);
}
else{
rows = paginationHelper.getRows();
}
} catch (NumberFormatException e) {
rows = 5;
}
try {
List<Commodity> commodityAll = DAOFactory.getICommodityDAOinstance().CommodityFindByUserid(user.getId());
paginationHelper.setTotalcount(commodityAll.size());
System.out.println(currentpage);
System.out.println("1+"+paginationHelper.getCurrentPageStart(currentpage));
System.out.println("2+"+paginationHelper.getCurrentPageEnd(currentpage));
paginationHelper.setCommodityAll(commodityAll.subList(paginationHelper.getCurrentPageStart(currentpage),paginationHelper.getCurrentPageEnd(currentpage)));
int totalpage = 0;
try {
totalpage = commodityAll.size() % paginationHelper.getRows() == 0 ? commodityAll.size() / paginationHelper.getRows() : commodityAll.size() / paginationHelper.getRows() + 1;
} catch (Exception e) {
totalpage = 0;
}
paginationHelper.setTotalpage(totalpage);
session.setAttribute("paginationHelper",paginationHelper);
session.setAttribute("commodityAll",commodityAll);
response.sendRedirect(path+"/JSP/ShoppingMall/MyShopping/MyShopping.jsp");
} catch (Exception e) {
response.sendRedirect(path+"/JSP/ShoppingMall/MyShopping/MyshoppingNull.jsp");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
}
3、page.jsp
//<!--需要源码+wx:yi3219727434 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="dao.vo.PaginationHelper" %><%--
Created by IntelliJ IDEA.
User: Look
Date: 2023/4/22
Time: 21:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="bootStrpJquery/css/bootstrap.min.css">
</head>
<body>
<div class="container" role="main">
<c:set var="commodityAll" value="${paginationHelper.getCommodityAll()}"/>
<c:set var="totalcount" value="${paginationHelper.getTotalcount()}"/>
<c:set var="pagination" value="${requestScope.pagination}"/>
<c:set var="currentpage" value="${paginationHelper.getCurrentpage()}"/>
<c:set var="totalPages" value="${paginationHelper.getTotalpage()}"/>
<c:set var="rows" value="${paginationHelper.getRows()}"/>
<div class="row justify-content-center">
<div class="col-md-8">
<form action="<c:url value="/pageServlet"/>" method="post">
<p>用户总数:${totalcount},每页用户数:<input name="rows" type="text"
value="${pagination.getCountPerPage()}"/>(<input
type="submit" value="修改"/>),总页数:${totalPages},当前页:${currentpage}</p>
</form>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-8 table-responsive">
<table class="table table-hover table-striped table-bordered table-sm">
<thead>
<tr>
<th>商品名称</th>
<th>商品id</th>
<th>商品产地</th>
<th>用户id</th>
<th>商品描述</th>
<th>商品标签</th>
<th>商品图像</th>
<th>商品价格</th>
</tr>
</thead>
<tbody>
<c:forEach var="commodity" items="${commodityAll}">
<tr>
<td>${commodity.getCname()}</td>
<td>${commodity.getCid()}</td>
<td>${commodity.getClocation()}</td>
<td>${commodity.getUserid()}</td>
<td>${commodity.getCdetails()}</td>
<td>${commodity.getClable()}</td>
<td><img src="../ClassHelpFile/Shopping/${commodity.getCimage()}"
style="width: 50px;height: 50px"></td>
<td>${commodity.getCprice()}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<div class="row justify-content-center">
<div>
<nav>
<ul class="pagination">
<li class="page-item"><a class="page-link" href="<c:url value="/pageServlet?currentpage=1"/>">首页</a></li>
<li class="page-item"><a class="page-link" href="<c:url value="/pageServlet?currentpage=${currentpage-1>1?currentpage-1:1}"/>">«</a>
</li>
<c:forEach begin="1" end="${totalPages}" varStatus="loop">
<c:set var="active" value="${loop.index==currentpage?'active':''}"/>
<li class="page-item ${active}">
<a class="page-link" href="<c:url value="/pageServlet?currentpage=${loop.index}"/>">${loop.index}</a>
</li>
</c:forEach>
<li class="page-item">
<a class="page-link" href="<c:url value="/pageServlet?currentpage=${currentpage+1<totalPages?currentpage+1:totalPages}"/>">»</a>
</li>
<li class="page-item">
<a class="page-link" href="<c:url value="/pageServlet?currentpage=${totalPages}"/>">尾页</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</body>
</html>