jsp电子商务 购物车实现之二 登录和分页篇
2016-12-04 17:39
232 查看
登录页面核心代码
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<Book> 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);
}
购物车页面显示代码段参考:<div id="content" class="wrap">
<div class="list bookList">
<form method="post" name="shoping" action="CartServlet" onsubmit="return checkCart();">
<table>
<tr>
<th class="checker">@</th>
<th>书名</th>
<th class="info">简介</th>
<th class="price">价格</th>
<th class="store">库存</th>
<th class="view">图片预览</th>
</tr>
<c:forEach items="${books}" var="book"><!--book.id的值可以存放value -->
<tr>
<td><input type="checkbox" name="bookId" value="${book.id}" /></td>
<td class="title">${book.bookname}</td>
<td class="info">${book.info}</td>
<td>¥${book.price}</td>
<td>${book.stock}</td>
<td class="thumb"><img src="images/book/${book.image}" /></td>
</tr>
</c:forEach>
</table>
<div class="page-spliter">
<a href="BooklistServlet?page=1">首页</a>
<c:if test="${pageindex>1}">
<a href="BooklistServlet?page=${pageindex-1}">上一页</a>
</c:if>
<c:if test="${pageindex<total}">
<a href="BooklistServlet?page=${pageindex+1}">下一页</a>
</c:if>
<a href="BooklistServlet?page=${total}">尾页</a>
</div>
<div class="button">
<input class="input-btn" type="submit"
name="submit" value="" /></div>
</form>
</div>
</div>
<div id="login"> <h2>用户登陆</h2> <form method="post" action="LoginServlet" onsubmit="return check()"> <dl> <dt>用户名:</dt> <dd><input class="input-text" type="text" id="username" name="username" onblur="isUsernameNull()"/><span id="usernull"></span></dd> <dt>密 码:</dt> <dd><input class="input-text" type="password" id="password" name="password" onblur="isPasswordNull()"/><span id="pwdnull"></span></dd> <dt> </dt> <dd class="button"><input class="input-btn" type="submit" name="submit" value="" /> <input class="input-reg" type="button" name="register" value="" onclick="window.location='register.jsp';" /></dd> </dl> </form> </div>
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<Book> 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);
}
购物车页面显示代码段参考:<div id="content" class="wrap">
<div class="list bookList">
<form method="post" name="shoping" action="CartServlet" onsubmit="return checkCart();">
<table>
<tr>
<th class="checker">@</th>
<th>书名</th>
<th class="info">简介</th>
<th class="price">价格</th>
<th class="store">库存</th>
<th class="view">图片预览</th>
</tr>
<c:forEach items="${books}" var="book"><!--book.id的值可以存放value -->
<tr>
<td><input type="checkbox" name="bookId" value="${book.id}" /></td>
<td class="title">${book.bookname}</td>
<td class="info">${book.info}</td>
<td>¥${book.price}</td>
<td>${book.stock}</td>
<td class="thumb"><img src="images/book/${book.image}" /></td>
</tr>
</c:forEach>
</table>
<div class="page-spliter">
<a href="BooklistServlet?page=1">首页</a>
<c:if test="${pageindex>1}">
<a href="BooklistServlet?page=${pageindex-1}">上一页</a>
</c:if>
<c:if test="${pageindex<total}">
<a href="BooklistServlet?page=${pageindex+1}">下一页</a>
</c:if>
<a href="BooklistServlet?page=${total}">尾页</a>
</div>
<div class="button">
<input class="input-btn" type="submit"
name="submit" value="" /></div>
</form>
</div>
</div>
相关文章推荐
- jsp电子商务 购物车实现之二 登录和分页篇
- jsp电子商务 购物车实现之二 登录和分页篇
- jsp电子商务 购物车实现之一 设计篇
- jsp电子商务 购物车实现之三 购物车
- jsp电子商务 购物车实现之一 设计篇
- jsp电子商务 购物车实现之一 设计篇
- jsp电子商务 购物车实现之三 购物车
- jsp电子商务 购物车实现之三 购物车
- 基于MVC 用户登录的实现(JavaBean + Servlet + JSP)
- (视频)玩转Membership视频之二:使用系列“登录控件”实现高效开发
- 用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。
- java jsp+servlet实现登录网页设计
- jsp通过Cookie实现自动登录
- JSP的隐式对象----实例,用模式一实现用户登录功能。
- Jsp 操作 Cookie 实现自动登录
- jsp通过Cookie实现自动登录
- 利用Servlet和jsp实现客户端与服务器端的用户登录信息验证
- jsp Filter 实现编码过滤,和登录验证
- jsp通过Cookie实现网站自动登录
- JSP+Servlet+JavaBean实现登录网页设计