分页
主要把数据存储通过session技术来保存,可以进行传递。
首页
<%@page import="com.zking.web.entity.Goods"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.web.dao.GoodsDao"%>
<%@page import="com.zking.web.entity.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 通过session默认保存一个用户 -->
<h3>zz购物商城首页</h3>
<p>欢迎您!${username }</p>
<p align="center">
<from action="doLoad.jsp" method="post">名称:<input type="text"
name="strName">
<button type="submit" value="搜索">搜索</button>
</from>
</p>
<table border="1" width="70%" align="center">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>描述</th>
<th>图片</th>
<th>操作</th>
</tr>
<c:if test="${empty requestScope.listGoods }">
<jsp:forward page="doLoad.jsp"></jsp:forward>
</c:if>
<c:forEach items="${requestScope.listGoods }" var="goods">
<tr>
<td>${goods.gid }</td>
<td>${goods.gname }</td>
<td>${goods.gprice }</td>
<td>${goods.ginfo }</td>
<td><img style="width: 80px; height: 50px;"
src="${goods.gpath }" /></td>
<td>
<button onclick="addCart(${goods.gid})">加入购物车</button>
</td>
</tr>
</c:forEach>
</table>
<p align="right" style="font-size: 20px; font-weight: bold">
当前页数:[${pageIndex }/${pageMax }] <a href="index.jsp?pageIndex=1">首页</a>
<a href="index.jsp?pageIndex=${ pageIndex-1<0?1:pageIndex-1}">上一页</a>
<a
href="index.jsp?pageIndex=${ pageIndex+1>pageMax?pageMax:pageIndex+1}">下一页</a>
<a href="index.jsp?pageIndex=${ pageMax }">末页</a>
</p>
<script type="text/javascript">
//加入购物车的点击事件
function addCart(cid){
//alert(cid)
location.href = "doShopping.jsp?cid="+cid;
}
</script>
</body>
</html>
do处理页面
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取信息
String username = request.getParameter("username");
String password = request.getParameter("password");
//3.数据库交互
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn = DriverManager.getConnection(url, "scott", "123");
//sql
String sql = "select * from tb_news_users where username = ? and password = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
//4.判断页面跳转
if(rs.next()){
//跳转到前端的首页
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{
out.println("<script>alert('账户或密码错误');location.href='login.jsp'</script>");
}