import java.util.List;
/**
* 分页功能接口
* @author feng@lu
* 2014-8-5 下午8:08:33
*/
public interface SplitPage {
//获取总记录数
int getTotalCount();
//获取总页数
int getTotalPage();
//按页获取记录
List queryPerPage(int page);
//设置每页显示的记录数
void setCountPerPage(int countPerPage);
}
============================
package com.teachingaward.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.teachingaward.vo.SystemUser;
/**
* 实现分页接口的类
* @author feng@lu
* 2014-8-5 下午8:15:32
*/
public class SplitPageTmp implements SplitPage {
private int countPerPage = 5;//每页显示的元素个数
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
@Override
//获取总记录数
public int getTotalCount() {
int totalCount = 0;//设置总记录数默认值为0
try {
conn = DB.getConnection();
String sql = "select count(*) from system_users where authority=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "申报员");
rs = pstmt.executeQuery();
if(rs.next()){
totalCount = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DB.free(rs, pstmt, conn);
}
return totalCount;
}
@Override
//获取总页数
public int getTotalPage() {
int totalCount = this.getTotalCount();
int totalPage = 0;
if(totalCount % countPerPage == 0){
totalPage = totalCount / countPerPage;
}else{
totalPage = totalCount / countPerPage + 1;
}
return totalPage;
}
@Override
//按页获取记录
public List<SystemUser> queryPerPage(int page) {
List<SystemUser> list = new ArrayList<SystemUser>();
int startRow = (page - 1) * countPerPage;//定义开始页起始元素
try {
conn = DB.getConnection();
String sql = "select * from system_users where authority=? limit ?, ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "申报员");
pstmt.setInt(2, startRow);
pstmt.setInt(3, countPerPage);
rs = pstmt.executeQuery();
while(rs.next()){
SystemUser user = new SystemUser();
user.setUsername(rs.getString("username"));
user.setName(rs.getString("name"));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DB.free(rs, pstmt, conn);
}
return list;
}
@Override
//设置每页显示的记录数
public void setCountPerPage(int countPerPage) {
this.countPerPage = countPerPage;
}
}
=======================================
package com.teachingaward.util;
/**
* 返回分页查询的dao-SplitPageTmp对象
* @author feng@lu
* 2014-8-5 下午8:33:20
*/
public class UserDao {
public static SplitPage getUserTMP(){
return new SplitPageTmp();
}
}
====================================
package com.teachingaward.supervise.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.teachingaward.util.UserDao;
import com.teachingaward.vo.SystemUser;
public class UserManagerServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//这种编码
request.setCharacterEncoding("utf-8");
SystemUser user = new SystemUser();
//获取action参数
// String action = request.getParameter("action");
// if("showAllUser".equals(action)){
int page = 1;
String p = request.getParameter("page");
if(p!=null){
page = Integer.parseInt(p);
}
int totalCount = UserDao.getUserTMP().getTotalCount();
int totalPage = UserDao.getUserTMP().getTotalPage();
List list = UserDao.getUserTMP().queryPerPage(page);
request.setAttribute("totalCount", new Integer(totalCount));
request.setAttribute("totalPage", new Integer(totalPage));
request.setAttribute("page", new Integer(page));
request.setAttribute("allList", list);
//转发
request.getRequestDispatcher("/WEB-INF/pages/supervise/Supervisory_Personnel.jsp").forward(request, response);
// }
}
}
=================================================
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>吉首大学本科教学工作奖励统计分析系统监督</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body style="margin: 0;padding: 0;">
<div class="main" style="width: 1000px;height: 580px;border-bottom-color: green;margin-left: 150px;" align="center">
<!-- 页面顶部导航 -->
<div class="top" style="width: 1000px; height: 100px;background-image: url('<%=path %>/images/top.jpg');" >
<h2>吉首大学本科教学工作奖励统计分析系统-监督</h2>欢迎您:${sessionScope.declareName }
</div>
<!-- 页面左部导航 -->
<div class="left" style="float: left;width: 200px; height: 400px;background-color: gray;margin-top: 2px;">
页面左部导航
</div>
<!-- 主要内容 -->
<div class="right" style="float:left;width: 800px; height: 400px;background-color: silver;margin-top: 2px;">
<%
int totalCount = (Integer)request.getAttribute("totalCount");
int totalPage = (Integer)request.getAttribute("totalPage");
int p = (Integer)request.getAttribute("page");
/* pageContext.setAttribute("p", p); */
%>
申报总人数:<font color="red">${requestScope.totalCount }</font>
当前页:<font color="red">${page }</font><%-- <%=p %>${p } --%>
总页数:<font color="red">${totalPage }</font>
<table border="1" width="65%" cellspacing="0" cellpadding="0">
<thead><h2>申报人员列表</h2></thead>
<tr style="text-align: center;">
<td>序号</td>
<td>工号</td>
<td>姓名</td>
<td>身份</td>
<td>操作</td>
</tr>
<c:forEach items="${allList }" var="u" varStatus="status">
<tr style="text-align: center;">
<td>${ status.index + 1}</td>
<td>${u.username }</td>
<td>${u.name }</td>
<td>申报员</td>
<td><a href="#" style="color: red;">审核</a></td>
</tr>
</c:forEach>
</table>
<!-- pagebar -->
<span>
<form action="servlet/UserManagerServlet" method="post">
跳转到<input type="text" name="page" size="3" alt="进入">页
<input type="submit" value=">>">
</form>
<%if(p==1){ %>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=1">首页</a>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=${page+1 }">下一页</a>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=${totalPage }">末页</a>
<%}if(p==totalPage){ %>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=${page-1 }">上一页</a>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=1">首页</a>
<%}if(p!=1 && p!=totalPage){ %>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=1">首页</a>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=${page-1 }">上一页</a>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=${page+1 }">下一页</a>
<a href="${pageContext.request.contextPath}/servlet/UserManagerServlet?page=${totalPage }">末页</a>
<%} %>
</span>
</div>
<!-- 网页底部 -->
<div class="footer" style="width: 1000px; height: 60px;background-color: #317d5f;margin-top: 404px;color: white; vertical-align: bottom;">
版权@LuFeng-吉首大学创新工作室------指导老师:QinZunYue
</div>
</div>
</body>
</html>
=================================
package com.teachingaward.controller;
/**
* 登录servlet控制器
* 2014/7/24
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.teachingaward.service.impl.LoginService;
import com.teachingaward.service.impl.SearchAllUserService;
import com.teachingaward.supervise.service.impl.FindAllDeclareUsers;
import com.teachingaward.vo.SystemUser;
public class ServletLogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
//获取登录用户的验证信息
String name = request.getParameter("username");
String pwd = request.getParameter("password");
String authority = request.getParameter("authority");
//调用service层
LoginService loginService = new LoginService();
boolean flag = false;
try {
flag = loginService.login(name, pwd, authority);
} catch (SQLException e) {
e.printStackTrace();
}
//跳转到不同的视图
if(flag){ //根据权限跳转到不同的视图
if("管理员".equals(authority)){ //管理员视图
// System.out.println("管理员登录");
//把申报员姓名存入session中,以便获取
request.getSession().setAttribute("declareName", name);
//①:查询所有的用户,存入list中
SearchAllUserService allUserService = new SearchAllUserService();
List<SystemUser> list = null;
try {
list = allUserService.searchAllUsers(name);
} catch (SQLException e) {
e.printStackTrace();
}
//②:必须把数据库中的所有用户查询出来,request.setAttribute("list", list);
request.setAttribute("userList", list);
//③:请求转发到显示所有用户的页面,取出request域中的list属性
request.getRequestDispatcher("/WEB-INF/pages/system_management/System_Administrator.jsp").forward(request, response);
}else if("申报员".equals(authority)){ //申报员视图
// System.out.println("申报员登录");
//把申报员姓名存入session中,以便获取
request.getSession().setAttribute("declareName", name);
//转发到申报首页
request.getRequestDispatcher("/WEB-INF/pages/declare/Dteacher_Award.jsp").forward(request, response);
}else if("规则员".equals(authority)){ //规则制定员视图
// System.out.println("规则制定员登录");
//把申报员姓名存入session中,以便获取
request.getSession().setAttribute("declareName", name);
//转发到规则制定首页
request.getRequestDispatcher("/WEB-INF/pages/rule/Rteacher_Award.jsp").forward(request, response);
}else if("监督员".equals(authority)){ //监督员视图
System.out.println("监督员登录");
//把申报员姓名存入session中,以便获取
request.getSession().setAttribute("declareName", name);
//①:查询所有的用户,存入list中
FindAllDeclareUsers allUserService = new FindAllDeclareUsers();
List<SystemUser> list = null;
list = allUserService.findAllUsers(name, authority);
//②:必须把数据库中的所有用户查询出来,request.setAttribute("list", list);
if(list!=null){
request.setAttribute("allList", list);
}
//③:请求转发到显示所有用户的页面,取出request域中的list属性
//转发到Servlet
request.getRequestDispatcher("/servlet/UserManagerServlet").forward(request, response);
//转发到监督首页
// request.getRequestDispatcher("/WEB-INF/pages/supervise/Supervisory_Personnel.jsp").forward(request, response);
}else{//"-1".equals(authority)----没必要判断,因为在数据查找中因为验证了authority等于"-1"
//这里面的内容永远得不到执行
}
}else{ //登录失败回到登录页面
response.sendRedirect("/"+ request.getContextPath() +"/login.jsp");
}
}
}
======================================================
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!-- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> -->
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>吉首大学本科教学工作奖励信息化系统</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jsfiles/validateLogin.js"></script>
</head>
<body >
<form action="servlet/ServletLogin" name="loginform" method="post">
<h1 align="center">用户登录</h1>
<div align="center">
用户名:<input type="text" name="username" placeholder="请输入正确的职工号" ><br><br>
密 码:<input type="password" name="password" placeholder="请输入正确的密码"><br><br>
权 限:<select name="authority" size="1">
<option value="-1">-选择权限进入本系统-</option>
<option value="管理员">管理员</option>
<option value="申报员">申报员</option>
<option value="规则员">规则员</option>
<option value="监督员">监督员</option>
</select><br><br>
<input type="reset" value="重置">
<input type="submit" value="登录" name="submit" οnclick="validateLogin();">
</div>
</form>
</body>
</html>