package com.work.action;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tool.DBToolSinglecon;
import com.work.bean.*;
/**
*
* 查看分类
*
*/
@SuppressWarnings("serial")
public class SortShow extends HttpServlet {
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// 总记录
int countSize = 0;
// 每页记录
int pageSize = 10;
// 总页数
int pageCount = 0;
// 当前页
int currentPage = 0;
// 当前页的起始记录
int currentPageBegin = 0;
// 当前记录集合
List<sortbean> list = new ArrayList<sortbean>();
// 初始化javabean对象
sortbean use = null;
String sql = "SELECT COUNT(*) FROM sorts;";
// 启用工具类DBToolSinglecon
DBToolSinglecon res = DBToolSinglecon.getInstance();
//调用工具类的查询方法
ResultSet rs = res.query(sql);
// 获得总记录
try {
if (rs.next()) {
countSize = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
// 获得总页数
pageCount = (countSize + pageSize - 1) / pageSize;
// 得到当前页数
String pages = request.getParameter("page");
String sqls = null;
//进行一系列的判断,定义相关的sql语句
if (pages == null || pages.equals("")) {
currentPage = 1;
sqls = "SELECT * FROM sorts limit 0," + pageSize + "";
} else {
currentPage = Integer.valueOf(pages);
if (currentPage <= 0) {
currentPage = 1;
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + "";
} else {
if (currentPage > pageCount) {
currentPage = pageCount;
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + ";";
} else {
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + ";";
}
}
}
//执行sql语句
ResultSet rss = res.query(sqls);
//把所有的分类塞进javaBean,然后存入list集合
try {
while (rss.next()) {
use = new sortbean();
use.setSort_id(Integer.valueOf(rss.getString("sort_id")));
use.setSort_name(rss.getString("sort_name"));
list.add(use);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 把当前的页数传到下一个页面
request.setAttribute("currentpage", currentPage);
// 把总页数传到下一个页面
request.setAttribute("pagecount", pageCount);
// 把list对象传到下一个页面
request.setAttribute("list", list);
request.getRequestDispatcher("/AdminAction/querysort.jsp").forward(
request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
}
}
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="
http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>显示页面</title>
</head>
<body background="C:\Documents and Settings\Administrator\桌面\1.jpg">`
<a href="<%=path%>/AdminAction/AdminAction.jsp"><font color="#0000FF">返回主页面</font>
<%--得到list数据集合的对象 --%>
<%
Object list = request.getAttribute("list");
Object pagecount = request.getAttribute("pagecount");
Object currentpage = request.getAttribute("currentpage");
String li = (String) session.getAttribute("error");
%>
<center>
<%
if (null != li) {
out.print("<font size=1 color='ff0000'>" + li + "</font>"
+ "<br><br>");
}
%>
<%--使用foreach把DB里的数据显示出来 --%>
<table border=1>
<tr>
<th bgcolor="#808080">
分类号
</th>
<th bgcolor="#808080">
分类名
</th>
<th bgcolor="#808080">
管理
</th>
</tr>
<c:forEach var="list" items="${list}">
<tr>
<td>
<c:out value="${list.sort_id}" escapeXml="false" />
</td>
<td>
<c:out value="${list.sort_name}" escapeXml="false" />
</td>
<td>
<a href="<%=basePath%>deletesort?id=${list.sort_id}"><font
color="#0000FF">删除</font> <br> </a>
</td>
</tr>
</c:forEach>
</table>
第<%=currentpage%>页 共<%=pagecount%>页
<a href="<%=path%>/pageshow?page=1">1</a>
<a href="<%=path%>/pageshow?page=2">2</a>
<a href="<%=path%>/pageshow?page=3">3</a>
<form action="<%=path%>/sortshow">
<input type="text" name="page" style="width: 20" />
<input type="submit" value="GO" />
</form>
</center>
</body>
</html>