//管理员类
public class Admin {
private int id;
private String username;
private String password;
}
//用户类
public class User {
private int id;
private String name;
private String gender;
private int age;
private String address;
private String qq;
private String email;
}
//分页对象类
public class PageBean {
private int totalCount; // 总记录数
private int totalPage ; // 总页码
private List list ; // 每页的数据
private int currentPage ; //当前页码
private int rows;//每页显示的记录数
}
1.2 登录页面
//切换验证码
管理员登录
${login_msg}
效果如图
1.3 LoginServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取数据
//2.1获取用户填写验证码
String verifycode = request.getParameter("verifycode");
//3.验证码校验
HttpSession session = request.getSession();
String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
session.removeAttribute("CHECKCODE_SERVER");//确保验证码一次性
if(!checkcode_server.equalsIgnoreCase(verifycode)){
//验证码不正确
//提示信息
request.setAttribute("login_msg","验证码错误!");
//跳转登录页面
request.getRequestDispatcher("/login.jsp").forward(request,response);
return;
}
Map map = request.getParameterMap();
//4.封装User对象
Admin admin = new Admin();
try {
BeanUtils.populate(admin,map);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
//5.调用Service查询
AdminService service = new AdminServiceImpl();
Admin loginAdmin = service.login(admin);
//6.判断是否登录成功
if(loginAdmin != null){
//登录成功
//将用户存入session
session.setAttribute("admin",loginAdmin);
//跳转页面
response.sendRedirect(request.getContextPath()+"/index.jsp");
}else{
//登录失败
//提示信息
request.setAttribute("login_msg","用户名或密码错误!");
//跳转登录页面
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
2. 首页
2.1 首页页面
${admin.username},欢迎您
查询所有用户信息
效果如图
3. 数据展示 3.1 展示页面
代码过多,此处就只展示部分。使用JSTL标签,将查询到的数据依次输出展示
${s.count}
${user.name}
${user.gender}
${user.age}
${user.address}
${user.qq}
${user.email}
修改
删除
大致的效果需达成如下图
4. 添加功能 4.1 功能分析 4.2 add.jsp此页面编写不做过多赘述,只需要注意,利用javaScript去校验输入信息是否合法
页面如图
4.3 AddUserServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取参数
Map map = request.getParameterMap();
//3.封装对象
User user = new User();
try {
BeanUtils.populate(user,map);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
//4.调用Service保存
UserService service = new UserServiceImpl();
service.addUser(user);
//5.跳转到userListServlet
response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}
5. 修改功能
5.1 功能分析
5.2 update.jsp
注意:修改信息需要验证表单输入是否合法,可在提交时,调用add.jsp页面下的check()方法验证
页面如图
5.3 UpdateUserServletprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取map
Map map = request.getParameterMap();
//3.封装对象
User user = new User();
try {
BeanUtils.populate(user,map);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
//4.调用Service修改
UserService service = new UserServiceImpl();
service.updateUser(user);
//5.跳转到查询所有Servlet
response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}
6. 删除功能
6.1 删除功能分析
6.1.2 DelUserServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取id
String id = request.getParameter("id");
//2.调用service删除
UserService service = new UserServiceImpl();
service.deleteUser(id);
//3.跳转到查询所有Servlet
response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}
6.2 删除选中条目功能分析
6.2.1 DelSelectedServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取所有id
String[] ids = request.getParameterValues("uid");
//2.调用service删除
UserService service = new UserServiceImpl();
service.delSelectedUser(ids);
//3.跳转查询所有Servlet
response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}
7. 分页功能
7.1 功能初步分析
7.2 具体步骤
7.3 FindUserByPageServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//1.获取参数
String currentPage = request.getParameter("currentPage");//当前页码
String rows = request.getParameter("rows");//每页显示条数
if (currentPage == null || "".equals(currentPage)) {
currentPage = "1";
}
if (rows == null || "".equals(rows)) {
rows = "5";
}
//获取条件查询参数
Map condition = request.getParameterMap();
//2.调用service查询
UserService service = new UserServiceImpl();
PageBean pb = service.findUserByPage(currentPage, rows, condition);
System.out.println(pb);
//3.将PageBean存入request
request.setAttribute("pb", pb);
request.setAttribute("condition", condition);//将查询条件存入request
//4.转发到list.jsp
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
8. 条件查询功能
8.1 功能分析
8.2 具体实现
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)