jsp電子商務 購物車實現之二 登陸和分頁篇

登陸頁面核心代碼
<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的參考代碼:html

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>
相關文章
相關標籤/搜索