博客
关于我
超市订单管理系统项目(2)——登录功能实现(后端)
阅读量:338 次
发布时间:2019-03-04

本文共 4217 字,大约阅读时间需要 14 分钟。

登录功能实现

1. 登录页面设置

登录页面应设置为 login.jsp,并在欢迎页中指定登录页面或通过 index.jsp 转发至 login.jsp。以下是通过 index.jsp 实现的转发方式:

2. 数据库访问接口

定义 LoginDao 接口,用于获取用户登录信息:

public interface LoginDao {    User getLogin(String userCode) throws SQLException;}

3. 数据库访问实现类

实现 LoginDao 接口的 LoginDaoImpl 类:

public class LoginDaoImpl implements LoginDao {    @Override    public User getLogin(String uCode) throws SQLException {        Connection conn = BaseDao.getConn();        if (conn != null) {            try {                PreparedStatement preparedStatement = conn.prepareStatement(                    "SELECT * FROM smbms_user WHERE userCode = ?");                preparedStatement.setString(1, uCode);                ResultSet resultSet = BaseDao.executeQ(conn, preparedStatement, null, null);                if (resultSet.next()) {                    User user = new User();                    user.setId(resultSet.getInt("id"));                    user.setUserCode(resultSet.getString("userCode"));                    user.setUserName(resultSet.getString("userName"));                    user.setUserPassword(resultSet.getString("userPassword"));                    // 其他字段获取和赋值                    return user;                }            } catch (SQLException e) {                e.printStackTrace();            }            BaseDao.close(conn, preparedStatement, null);        }        return null;    }}

4. 业务逻辑接口

定义 LoginService 接口,用于处理登录业务:

public interface LoginService {    User login(String uCode, String uPassword) throws SQLException;}

5. 业务逻辑实现类

实现 LoginService 接口的 LoginServiceImpl 类:

public class LoginServiceImpl implements LoginService {    @Override    public User login(String uCode, String uPassword) throws SQLException {        LoginDao loginDao = new LoginDaoImpl();        User user = loginDao.getLogin(uCode);        return user;    }}

6. 登录Servlet实现

创建 LoginServlet 类,处理登录请求:

@WebServlet("/login.do")public class LoginServlet extends HttpServlet {    @Override    protected void service(HttpServletRequest req, HttpServletResponse resp)        throws ServletException, IOException {        String userCode = req.getParameter("userCode");        String password = req.getParameter("userPassword");        LoginService loginService = new LoginServiceImpl();        try {            User user = loginService.login(userCode, password);            if (user != null) {                HttpSession session = req.getSession();                session.setAttribute(Constants.USER_SESSION, user);                resp.sendRedirect("frame.jsp");            } else {                req.setAttribute("error", "用户名或密码错误");                req.getRequestDispatcher("login.jsp").forward(req, resp);            }        } catch (SQLException e) {            e.printStackTrace();        }    }}

7. 登出功能实现

创建 LogoutServlet 类,处理注销操作:

@WebServlet("/user/exit.do")public class LogoutServlet extends HttpServlet {    @Override    protected void service(HttpServletRequest req, HttpServletResponse resp)        throws ServletException, IOException {        HttpSession session = req.getSession();        session.removeAttribute(Constants.USER_SESSION);        resp.sendRedirect("/login.jsp");    }}

8. 登录拦截优化

创建 SysFilter 类,实现登录拦截:

@WebFilter("/jsp/*")public class SysFilter implements Filter {    @Override    public void init(FilterConfig filterConfig) throws ServletException {        // 初始化逻辑可根据需求添加    }    @Override    public void doFilter(        ServletRequest servletRequest,        ServletResponse servletResponse,        FilterChain filterChain)        throws IOException, ServletException {        HttpServletRequest request = (HttpServletRequest) servletRequest;        HttpServletResponse response = (HttpServletResponse) servletResponse;        HttpSession session = request.getSession();        User user_sess = (User) session.getAttribute(Constants.USER_SESSION);        if (user_sess == null) {            request.getRequestDispatcher("/error.jsp").forward(request, response);        } else {            filterChain.doFilter(servletRequest, servletResponse);        }    }    @Override    public void destroy() {        // 销毁逻辑可根据需求添加    }}

总结

以上实现完整地完成了登录功能的开发,涵盖了页面转发、数据库访问、业务逻辑处理以及前后端的整合。通过拦截器优化,确保未登录用户无法访问核心功能,进一步提升了系统安全性。

转载地址:http://ulgh.baihongyu.com/

你可能感兴趣的文章
NTP配置
查看>>
NUC1077 Humble Numbers【数学计算+打表】
查看>>
NuGet Gallery 开源项目快速入门指南
查看>>
NuGet(微软.NET开发平台的软件包管理工具)在VisualStudio中的安装的使用
查看>>
nuget.org 无法加载源 https://api.nuget.org/v3/index.json 的服务索引
查看>>
Nuget~管理自己的包包
查看>>
NuGet学习笔记001---了解使用NuGet给net快速获取引用
查看>>
nullnullHuge Pages
查看>>
NullPointerException Cannot invoke setSkipOutputConversion(boolean) because functionToInvoke is null
查看>>
null可以转换成任意非基本类型(int/short/long/float/boolean/byte/double/char以外)
查看>>
Number Sequence(kmp算法)
查看>>
Numix Core 开源项目教程
查看>>
numpy
查看>>
Numpy 入门
查看>>
NumPy 库详细介绍-ChatGPT4o作答
查看>>
NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值
查看>>
numpy 或 scipy 有哪些可能的计算可以返回 NaN?
查看>>
numpy 数组 dtype 在 Windows 10 64 位机器中默认为 int32
查看>>
numpy 数组与矩阵的乘法理解
查看>>
NumPy 数组拼接方法-ChatGPT4o作答
查看>>