以“tomcat运行5”为基础
一、搜索
在“IUserDao”下增加代码
List<User> search(String username);
在“UserDaoImpl”下增加代码
@Override
public List<User> search(String username) {
List<User> users=new ArrayList<>();
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
connection=DBUtil.getConnection();
String sql="select * from staff 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 (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return users;
}
在“IUserService”下增加代码
List<User> search(String username);
在“UserServiceImpl”下增加代码
@Override
public List<User> search(String username) {
return userDao.search(username);
}
在“servlet”包里新建“SearchServlet.java”,写入代码
package com.zhongruan.servlet;
import com.zhongruan.model.User;
import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.UserServiceImpl;
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;
import java.util.List;
public class SearchServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IUserService userService=new UserServiceImpl();
String username=request.getParameter("username");
List<User> users=userService.search(username);
HttpSession session=request.getSession();
session.setAttribute("pageInfo",users);
request.getRequestDispatcher("/allUser.jsp").forward(request,response);
}
}
在“web.xml”下增加代码
<servlet>
<servlet-name>SearchServlet</servlet-name>
<servlet-class>com.zhongruan.servlet.SearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchServlet</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
二、分页
在“IUserDao”下增加代码
List<User> findbyupage(int start,int size);
int selectCount();
在“UserDaoImpl”下增加代码
@Override
public List<User> findbyupage(int start, int size) {
List<User> users=new ArrayList<>();
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
connection=DBUtil.getConnection();
String sql="select * from staff 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 (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return users;
}
@Override
public int selectCount() {
List<User> users=new ArrayList<>();
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
int count=0;
try {
connection=DBUtil.getConnection();
String sql="select count(*) from staff";
statement=connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while (resultSet.next()){
count=resultSet.getInt(1);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return count;
}
在“IUserService”下增加代码
PageUInfo findbyupage(int currentPage,int size);
在“UserServiceImpl”下增加代码
@Override
public PageUInfo findbyupage(int currentPage, int size) {
PageUInfo pageUInfo=new PageUInfo();
pageUInfo.setCurrentPage(currentPage);
pageUInfo.setSize(size);
int start=(currentPage-1)*size;
List<User> users=userDao.findbyupage(start,size);
pageUInfo.setList(users);
int count=userDao.selectCount();
pageUInfo.setTotalCount(count);
Double ceil=Math.ceil(Double.valueOf(count)/size);
pageUInfo.setTotalPage(ceil.intValue());
return pageUInfo;
}
在“zhongruan”包里新建“pojo”包,在“pojo”包里新建“PageUInfo.java”,写入代码,并使用“Getter and Setter”方法和“toString”方法
private int currentPage;
private int size;
private List<User> list;
private int totalPage;
private int totalCount;
在“servlet”包里新建“FindByUPageServlet.java”,写入代码
package com.zhongruan.servlet;
import com.zhongruan.pojo.PageUInfo;
import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.UserServiceImpl;
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 FindByUPageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String current=request.getParameter("currentPage");
int currentPage=Integer.parseInt(current);
int size=Integer.parseInt(request.getParameter("size"));
IUserService userService=new UserServiceImpl();
PageUInfo pageUInfo=userService.findbyupage(currentPage,size);
HttpSession session=request.getSession();
session.setAttribute("pageInfo",pageUInfo);
request.getRequestDispatcher("/allUser.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int currentPage=1;
int size=5;
IUserService userService=new UserServiceImpl();
PageUInfo pageUInfo=userService.findbyupage(currentPage,size);
HttpSession session=request.getSession();
session.setAttribute("pageInfo",pageUInfo);
request.getRequestDispatcher("/allUser.jsp").forward(request,response);
}
}
在“web.xml”下增加代码
<servlet>
<servlet-name>FindByUPageServlet</servlet-name>
<servlet-class>com.zhongruan.servlet.FindByUPageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FindByUPageServlet</servlet-name>
<url-pattern>/findbyupage</url-pattern>
</servlet-mapping>
在“allUser”中修改代码
<c:forEach items="${pageInfo.list}" var="user">
在“allUser”的最后一个div中增加代码
<div>
<a href="/findbyupage?currentPage=1&size=5">首页</a>
1 2 3
<a href="/findbyupage?currentPage=${pageInfo.totalPage}&size=5">尾页</a>
</div>
实现效果