项目来源:新星计划2023【JavaWeb实现ATM机存取款项目实战】 学习方向报名入口-CSDN社区
目录
管理员登录
管理员登录逻辑
1、获取前端账号和密码
2、通过账号和密码去数据库中查找
3、根据查询结果判断
查询结果为null --> 账号或密码错误
else --> 跳转到后台页面
管理员登录主程序
功能实现
- 实现参数查询
- 业务层调用
@WebServlet("/login") -- > 连接页面
代码如下:
package com.atm.servlet;
import com.atm.pojo.Manager;
import com.atm.service.AdminUserService;
import com.atm.service.impl.AdminUserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获得参数
String name = request.getParameter("name");
String password = request.getParameter("password");
//2、调用业务层
AdminUserService adminUserService = new AdminUserServiceImpl();
try {
Manager manager = adminUserService.findManager(name, password);
if (manager == null) {
//登录失败
request.setAttribute("error", "用户登录失败");
request.getRequestDispatcher("changepage/login.jsp").forward(request, response);
} else {
//登陆成功,跳转到后台用户页面
response.sendRedirect("list");
}
}catch (Exception exception){
exception.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
用户信息封装
实现功能
将用户信息封装为一个对象,调用数据更加清晰
代码实现:
package com.atm.pojo;
public class Manager {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
业务层代码具体实现
功能实现
- 业务接口 --> 根据用户和密码在mysql中查询
- 业务实现类 --> 传入账号和密码返回用户信息
业务接口代码:
package com.atm.service;
import com.atm.pojo.Manager;
public interface AdminUserService {
/*根据账户,密码查询管理员*/
public Manager findManager(String name, String password) throws Exception;
}
业务实现类代码:
package com.atm.service.impl;
import com.atm.dao.AdminUserDao;
import com.atm.pojo.Manager;
import com.atm.service.AdminUserService;
/*管理员业务实现类*/
public class AdminUserServiceImpl implements AdminUserService {
private AdminUserDao adminUserDao = new AdminUserDao();
@Override
public Manager findManager(String name, String password) throws Exception{
return adminUserDao.findManager(name,password);
}
}
用户持久层操作
功能实现
实现业务层的具体操作
- 在MySQL中查询账号和密码
- 判断否为空
- null : 返回null
- no null : 返回Mysql中查询的用户信息
持久层代码:
package com.atm.dao;
import com.atm.pojo.Manager;
import com.atm.utill.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//管理员用户持久层操作
public class AdminUserDao {
/*逻辑
* 1在mysql中根据name和password查询信息
* 2如果存在将用户信息封装manager对象[id,name,password]返回
* 否则返回null*/
public Manager findManager(String name , String password) throws Exception{
Connection connection = DBUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select * from manage where name =? and password=?");
preparedStatement.setString(1,name);
preparedStatement.setString(2,password);
ResultSet resultSet = preparedStatement.executeQuery();
Manager manager = null;
if (resultSet.next()){
manager = new Manager();
manager.setId(resultSet.getInt(1));
manager.setName(resultSet.getString(2));
manager.setPassword(resultSet.getString(3));
}
return manager;
}
}
运行结果如下:
查询结果为空:
登录成功:
用户查询--》未完成
管理员登录后跳转到后台界面list,实现后台页面页面用户查询功能