用户登陆
LoginServlet的参考代码:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); UserinfoDao ud = new UserinfoDaoImpl(); Userinfo userinfo = ud.findByNameAndPwd(username, password); //如果登陆成功,则去bookListServlet if(userinfo!=null){ request.getSession().setAttribute("userinfo", userinfo); response.sendRedirect("BooklistServlet"); } else{ response.sendRedirect("login.jsp"); } }BooklistServlet的参考代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String page = req.getParameter("page"); if(page==null){ page="1";//第一次传过来就是默认首页 } int pageindex = Integer.parseInt(page);//否则,可能是第2页等 BookDao bd = new BookDaoImpl(); List购物车页面显示代码段参考:books = bd.findBookByPage(pageindex, 3);//3:每页多少数据 int count = bd.count(); PageControler pc = new PageControler(); int total = pc.getTotalPages(count, 3);//总页数 //通过request设置属性,+forward转向; req.setAttribute("pageindex", pageindex);// HttpSession session=req.getSession();// session.setAttribute("books",books); 也可以,可以直接跳转; req.setAttribute("books",books); req.setAttribute("total",total); req.getRequestDispatcher("index.jsp").forward(req,resp); }