登录前:
图
登录
登陆后
代码实现--登录状态和商品列表展示
1登录
LoginServlet
//只获取用户名,不获取密码验证等等
String username = request.getParameter("username");
request.getSession().setAttribute("username", username);
response.sendRedirect(request.getContextPath()+"/productList");
login.jsp
<form action="/WEB18/loginServlet" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
header.jsp
<div class="col-md-3" style="padding-top:20px">
<ol class="list-inline">
<c:if test="${empty username}">
<li><a href="login.jsp">登录</a></li>
<li><a href="register.jsp">注册</a></li>
</c:if>
<c:if test="${!empty username}">
<li>${username}</li>
<li><a href="#">退出</a></li>
</c:if>
<li><a href="cart.jsp">购物车</a></li>
<li><a href="order_list.jsp">我的订单</a></li>
</ol>
</div>
2,商品列表product
ProductListServlet
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product";
List<Product> productList = runner.query(sql, new BeanListHandler<Product>(Product.class));
request.setAttribute("productList", productList);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
product_list.jsp
<!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include>
<div class="row" style="width: 1210px; margin: 0 auto;">
<c:forEach items="${productList}" var="product">
<div class="col-md-2" style="height: 250px">
<a href="product_info.htm">
<img src="${pageContext.request.contextPath }/${product.pimage }" width="170" height="170" style="display: inline-block;">
</a>
<p>
<a href="product_info.html" style='color: green'>${product.pname }</a>
</p>
<p>
<font color="#FF0000">商城价:¥${product.shop_price }</font>
</p>
</div>
</c:forEach>
要配置jstl架包
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>