jsp电子商务 购物车实现之二 登录和分页篇

举报
tea_year 发表于 2021/12/29 23:57:42 2021/12/29
【摘要】 登录页面核心代码 <div id="login"> <h2>用户登陆</h2> <form method="post" action="LoginServlet" οnsubmit="return check()"> <dl> <dt>用户名:<...

  
  1. <div id="login">
  2. <h2>用户登陆</h2>
  3. <form method="post" action="LoginServlet" οnsubmit="return check()">
  4. <dl>
  5. <dt>用户名:</dt>
  6. <dd><input class="input-text" type="text" id="username" name="username" οnblur="isUsernameNull()"/><span id="usernull"></span></dd>
  7. <dt>密 码:</dt>
  8. <dd><input class="input-text" type="password" id="password" name="password" οnblur="isPasswordNull()"/><span id="pwdnull"></span></dd>
  9. <dt> </dt>
  10. <dd class="button"><input class="input-btn" type="submit" name="submit" value="" />
  11. <input class="input-reg" type="button" name="register"
  12. value="" οnclick="window.location='register.jsp';" /></dd>
  13. </dl>
  14. </form>
  15. </div>

LoginServlet的参考代码:


  
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. String username = request.getParameter("username");
  4. String password = request.getParameter("password");
  5. UserinfoDao ud = new UserinfoDaoImpl();
  6. Userinfo userinfo = ud.findByNameAndPwd(username, password);
  7. //如果登陆成功,则去bookListServlet
  8. if(userinfo!=null){
  9. request.getSession().setAttribute("userinfo", userinfo);
  10. response.sendRedirect("BooklistServlet");
  11. }
  12. else{
  13. response.sendRedirect("login.jsp");
  14. }
  15. }


  
  1. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  2. throws ServletException, IOException {
  3. String page = req.getParameter("page");
  4. if(page==null){
  5. page="1";//第一次传过来就是默认首页
  6. }
  7. int pageindex = Integer.parseInt(page);//否则,可能是第2页等
  8. BookDao bd = new BookDaoImpl();
  9. List<Book> books = bd.findBookByPage(pageindex, 3);//3:每页多少数据
  10. int count = bd.count();
  11. PageControler pc = new PageControler();
  12. int total = pc.getTotalPages(count, 3);//总页数
  13. //通过request设置属性,+forward转向;
  14. req.setAttribute("pageindex", pageindex);
  15. // HttpSession session=req.getSession();
  16. // session.setAttribute("books",books); 也可以,可以直接跳转;
  17. req.setAttribute("books",books);
  18. req.setAttribute("total",total);
  19. req.getRequestDispatcher("index.jsp").forward(req,resp);
  20. }


  
  1. <div id="content" class="wrap">
  2. <div class="list bookList">
  3. <form method="post" name="shoping" action="CartServlet" οnsubmit="return checkCart();">
  4. <table>
  5. <tr>
  6. <th class="checker">@</th>
  7. <th>书名</th>
  8. <th class="info">简介</th>
  9. <th class="price">价格</th>
  10. <th class="store">库存</th>
  11. <th class="view">图片预览</th>
  12. </tr>
  13. <c:forEach items="${books}" var="book"><!--book.id的值可以存放value -->
  14. <tr>
  15. <td><input type="checkbox" name="bookId" value="${book.id}" /></td>
  16. <td class="title">${book.bookname}</td>
  17. <td class="info">${book.info}</td>
  18. <td>¥${book.price}</td>
  19. <td>${book.stock}</td>
  20. <td class="thumb"><img src="images/book/${book.image}" /></td>
  21. </tr>
  22. </c:forEach>
  23. </table>
  24. <div class="page-spliter">
  25. <a href="BooklistServlet?page=1">首页</a>
  26. <c:if test="${pageindex>1}">
  27. <a href="BooklistServlet?page=${pageindex-1}">上一页</a>
  28. </c:if>
  29. <c:if test="${pageindex<total}">
  30. <a href="BooklistServlet?page=${pageindex+1}">下一页</a>
  31. </c:if>
  32. <a href="BooklistServlet?page=${total}">尾页</a>
  33. </div>
  34. <div class="button">
  35. <input class="input-btn" type="submit"
  36. name="submit" value="" /></div>
  37. </form>
  38. </div>
  39. </div>



文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/53455314

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。