1.在“基于servlet+jsp框架的管理系统”基础上继续修改,删除FindServlet.java和SearchServlet.java。
2.将FindByPageServlet.java内容修改为以下内容:
package com.zhongruan.servlet;
import com.zhongruan.pojo.PageInfo;
import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.UserServiceImpl;
import javax.management.DynamicMBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class FindByPageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session=req.getSession();
String searchname = (String) session.getAttribute("searchname");
String current=req.getParameter("currentPage");
int currentPage=Integer.parseInt(current);
String size1=req.getParameter("size");
int size=Integer.parseInt(size1);
IUserService userService=new UserServiceImpl();
PageInfo pageInfo=userService.findByPage(searchname,currentPage,size);
session.setAttribute("pageInfo",pageInfo);
req.getRequestDispatcher("allUser.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String searchname=req.getParameter("searchname");
int currentPage=1;
int size=5;
IUserService userService=new UserServiceImpl();
PageInfo pageInfo=userService.findByPage(searchname,currentPage,size);
HttpSession session=req.getSession();
session.setAttribute("searchname",searchname);
session.setAttribute("pageInfo",pageInfo);
req.getRequestDispatcher("allUser.jsp").forward(req,resp);
}
}
3.修改IUserDao内容:
将List search(String username); ListfindByPage(int start,int size);删除,
添加ListfindByPage(String username,int start,int size);
4.修改UserDaoImpl内容:
将以下内容删除
@Override
public List<User> search(String username) {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List<User>users = new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "select * from account where username like ?";
statement = connection.prepareStatement(sql);
statement.setString(1,"%"+username+"%");
resultSet = statement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
users.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeALL(resultSet, statement, connection);
}
return users;
}
@Override
public List<User> findByPage(int start, int size) {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List<User>users = new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "select * from account limit ?,?";
statement = connection.prepareStatement(sql);
statement.setInt(1,start);
statement.setInt(2,size);
resultSet = statement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
users.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeALL(resultSet, statement, connection);
}
return users;
}
@Override
public int seletCount() {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List<User>users = new ArrayList<>();
int count=0;
try {
connection = DBUtil.getConnection();
String sql = "select count(*) from account ";
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while (resultSet.next()) {
count=resultSet.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeALL(resultSet, statement, connection);
}
return count;
}
添加如下代码:
@Override
public List<User> findByPage(String username,int start, int size) {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List<User>users = new ArrayList<>();
try {
connection = DBUtil.getConnection();
if(username==null){
String sql = "select * from account limit ?,?";
statement = connection.prepareStatement(sql);
statement.setInt(1,start);
statement.setInt(2,size);
}else {
String sql = "select * from account where username like ? limit ?,?";
statement = connection.prepareStatement(sql);
statement.setString(1,"%"+username+"%");
statement.setInt(2,start);
statement.setInt(3,size);
}
resultSet = statement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
users.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeALL(resultSet, statement, connection);
}
return users;
}
@Override
public int seletCount(String searchname) {
List<User> users=new ArrayList<>();
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
int count=0;
try {
connection = DBUtil.getConnection();
if(searchname==null){
String sql = "select count(*) from account ";
statement = connection.prepareStatement(sql);
}else{
String sql = "select count(*) from account where username like ?";
statement = connection.prepareStatement(sql);
statement.setString(1,"%" + searchname + "%");
}
resultSet = statement.executeQuery();
while (resultSet.next()) {
count=resultSet.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeALL(resultSet, statement, connection);
}
return count;
}
5.修改IUserService内容:
删除Listsearch(String username); PageInfo findByPage(int currentPage,int size);
添加PageInfo findByPage(String username,int currentPage,int size);
6.修改UserService内容:
将以下内容删除
@Override
public List<User> search(String username) {
return userDao.search(username);
}
@Override
public PageInfo findByPage(int currentPage, int size) {
PageInfo pageInfo=new PageInfo();
pageInfo.setCurrentPage(currentPage);
pageInfo.setSize(size);
int start=(currentPage-1)*size;
List<User>users=userDao.findByPage(start,size);
pageInfo.setList(users);
int count=userDao.seletCount();
pageInfo.setTotalCount(count);
Double c=Double.valueOf(count);
Double ceil=Math.ceil(c/size);
pageInfo.setTotalPage(ceil.intValue());
return pageInfo;
}
添加以下代码:
@Override
public PageInfo findByPage(String searchname,int currentPage, int size) {
PageInfo pageInfo=new PageInfo();
pageInfo.setCurrentPage(currentPage);
pageInfo.setSize(size);
int start=(currentPage-1)*size;
List<User>users=userDao.findByPage(searchname,start,size);
pageInfo.setList(users);
int count=userDao.seletCount(searchname);
pageInfo.setTotalCount(count);
Double c=Double.valueOf(count);
Double ceil=Math.ceil(c/size);
pageInfo.setTotalPage(ceil.intValue());
return pageInfo;
}