资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

【Maven基础】单一架构案例(三)-创新互联

第六节 业务功能:登录 1、显示首页 1.1、流程图

在这里插入图片描述

成都创新互联专注于企业营销型网站建设、网站重做改版、从江网站定制设计、自适应品牌网站建设、H5开发商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为从江等各大城市提供网站开发制作服务。1.2、创建 PortalServlet 1.2.1、创建 Java 类

在这里插入图片描述

public class PortalServlet extends ViewBaseServlet {@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 声明要访问的首页的逻辑视图
        String templateName = "index";
        
        // 调用父类的方法根据逻辑视图名称渲染视图
        processTemplate(templateName, req, resp);
    }
}
1.2.2、注册

在这里插入图片描述

portalServletcom.atguigu.imperial.court.servlet.module.PortalServletportalServlet/
1.3、在 index.html 中编写登录表单

在这里插入图片描述

Title

账号:
密码:
2、登录操作 2.1、流程图

在这里插入图片描述

2.2、创建 EmpService

在这里插入图片描述

2.3、创建登录失败异常

在这里插入图片描述

public class LoginFailedException extends RuntimeException {public LoginFailedException() {}

    public LoginFailedException(String message) {super(message);
    }

    public LoginFailedException(String message, Throwable cause) {super(message, cause);
    }

    public LoginFailedException(Throwable cause) {super(cause);
    }

    public LoginFailedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {super(message, cause, enableSuppression, writableStackTrace);
    }
}
2.4、增加常量声明

在这里插入图片描述

public class ImperialCourtConst {public static final String LOGIN_FAILED_MESSAGE = "账号、密码错误,不可进宫!";
    public static final String ACCESS_DENIED_MESSAGE = "宫闱禁地,不得擅入!";
    public static final String LOGIN_EMP_ATTR_NAME = "loginInfo";

}
2.5、创建 AuthServlet 2.5.1、创建 Java 类

在这里插入图片描述

public class AuthServlet extends ModelBaseServlet {private EmpService empService = new EmpServiceImpl();

    protected void login(
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {try {// 1、获取请求参数
            String loginAccount = request.getParameter("loginAccount");
            String loginPassword = request.getParameter("loginPassword");

            // 2、调用 EmpService 方法执行登录逻辑
            Emp emp = empService.getEmpByLoginAccount(loginAccount, loginPassword);

            // 3、通过 request 获取 HttpSession 对象
            HttpSession session = request.getSession();

            // 4、将查询到的 Emp 对象存入 Session 域
            session.setAttribute(ImperialCourtConst.LOGIN_EMP_ATTR_NAME, emp);

            // 5、前往指定页面视图
            String templateName = "temp";
            processTemplate(templateName, request, response);

        } catch (Exception e) {e.printStackTrace();

            // 6、判断此处捕获到的异常是否是登录失败异常
            if (e instanceof LoginFailedException) {// 7、如果是登录失败异常则跳转回登录页面
                // ①将异常信息存入请求域
                request.setAttribute("message", e.getMessage());

                // ②处理视图:index
                processTemplate("index", request, response);

            }else {// 8、如果不是登录异常则封装为运行时异常继续抛出
                throw new RuntimeException(e);

            }

        }

    }
}
2.5.2、注册

在这里插入图片描述

authServletcom.atguigu.imperial.court.servlet.module.AuthServletauthServlet/auth
2.6、EmpService 方法

在这里插入图片描述

public class EmpServiceImpl implements EmpService {private EmpDao empDao = new EmpDaoImpl();

    @Override
    public Emp getEmpByLoginAccount(String loginAccount, String loginPassword) {// 1、对密码执行加密
        String encodedLoginPassword = MD5Util.encode(loginPassword);

        // 2、根据账户和加密密码查询数据库
        Emp emp = empDao.selectEmpByLoginAccount(loginAccount, encodedLoginPassword);

        // 3、检查 Emp 对象是否为 null
        if (emp != null) {//	①不为 null:返回 Emp
            return emp;
        } else {//	②为 null:抛登录失败异常
            throw new LoginFailedException(ImperialCourtConst.LOGIN_FAILED_MESSAGE);
        }
    }
}
2.7、EmpDao 方法

在这里插入图片描述

public class EmpDaoImpl extends BaseDaoimplements EmpDao {@Override
    public Emp selectEmpByLoginAccount(String loginAccount, String encodedLoginPassword) {// 1、编写 SQL 语句
        String sql = "select emp_id empId," +
                "emp_name empName," +
                "emp_position empPosition," +
                "login_account loginAccount," +
                "login_password loginPassword " +
                "from t_emp where login_account=? and login_password=?";

        // 2、调用父类方法查询单个对象
        return super.getSingleBean(sql, Emp.class, loginAccount, encodedLoginPassword);
    }
}
2.8、临时页面

在这里插入图片描述

临时

3、退出登录 3.1、在临时页面编写超链接

在这里插入图片描述

退朝
3.2、在 AuthServlet 编写退出逻辑

在这里插入图片描述

protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1、通过 request 对象获取 HttpSession 对象
    HttpSession session = request.getSession();

    // 2、将 HttpSession 对象强制失效
    session.invalidate();

    // 3、回到首页
    String templateName = "index";
    processTemplate(templateName, request, response);
}

本文章参考B站 尚硅谷2022版Maven教程(maven入门+高深,全网无出其右!),仅供个人学习使用,部分内容为本人自己见解,与尚硅谷无关。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享文章:【Maven基础】单一架构案例(三)-创新互联
URL标题:http://cdkjz.cn/article/ccdicj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220