页面本身刷新流程:在事件中写入跳转的servlet文件(get,post都行)执行逻辑判断,并获得前端的值或设置前端的值,再跳转回jps 1.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%--c方法--%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Title</title> </head> <body> <span> 登录成功</span> <table> <tr> <th>编号</th> <th>id</th> <th>名称</th> <th>数量</th> <th>进库价格</th> <th>出库价格</th> </tr> <%-- 接收后端传来的数据--%> <%-- 遍历 名字--%> <c:forEach items="${stocklist}" var ="sto" varStatus="stostatus"> <%-- index当前行的下标--%> <%-- .current 当前这一行的数据--%> <tr> <td>${stostatus.index+1}</td> <td>${stostatus.current.id}</td> <td>${stostatus.current.goods_name}</td> <td>${stostatus.current.goods_num}</td> <td>${stostatus.current.goods_in_price}</td> <td>${stostatus.current.goods_out_price}</td> </tr> <%-- <tr>--%> <%-- <td>${sto.id}</td>--%> <%-- <td>${sto.goods_name}</td>--%> <%-- <td>${sto.goods_num}</td>--%> <%-- <td>${sto.goods_in_price}</td>--%> <%-- <td>${sto.goods_out_price}</td>--%> <%-- </tr>--%> </c:forEach> </table> <button οnclick="location.href='/ServletLG/ServletSelect?currentPage= 1'">首页</button> <button οnclick="location.href='/ServletLG/ServletSelect?currentPage=${currentPage-1}'">上一页</button> <button οnclick="location.href='/ServletLG/ServletSelect?currentPage=${currentPage+1}'">下一页</button> <button οnclick="location.href='/ServletLG/ServletSelect?currentPage=${maxye}'">尾页</button> </body> <%--<script src="../ajax/fenYe.js"></script>--%> </html>
2.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StockDao sd=new StockDao(); // 获取当前页数 int tiaoshu=3; // request.getParameter("currentPage") 返回的值可能带有空格 用trim 去除前后空格 int currentPage_as = Integer.parseInt(request.getParameter("currentPage").trim()); int maxPage=sd.maxPage(tiaoshu); if (currentPage_as<1){ currentPage_as=1; }else if(currentPage_as>maxPage){ currentPage_as=maxPage; } System.out.println("currentPage_as="+ currentPage_as); //首页 System.out.println(maxPage); // 将最大页数返回前端 request.getSession().setAttribute("maxye",maxPage); List<Stock> ls2=sd.getFenYe(tiaoshu,currentPage_as); // 回传 request.getSession().setAttribute("stocklist",ls2); request.getSession().setAttribute("currentPage",currentPage_as); request.getRequestDispatcher("/jsp/denglutrue.jsp").forward(request,response); }