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";
// 調(diào)用父類的方法根據(jù)邏輯視圖名稱渲染視圖
processTemplate(templateName, req, resp);
}
}
1.2.2、注冊portalServlet com.atguigu.imperial.court.servlet.module.PortalServlet portalServlet /
1.3、在 index.html 中編寫登錄表單Title
2、登錄操作
2.1、流程圖2.2、創(chuàng)建 EmpService2.3、創(chuàng)建登錄失敗異常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、創(chuàng)建 AuthServlet
2.5.1、創(chuàng)建 Java 類public class AuthServlet extends ModelBaseServlet {private EmpService empService = new EmpServiceImpl();
protected void login(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {try {// 1、獲取請求參數(shù)
String loginAccount = request.getParameter("loginAccount");
String loginPassword = request.getParameter("loginPassword");
// 2、調(diào)用 EmpService 方法執(zhí)行登錄邏輯
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、如果是登錄失敗異常則跳轉(zhuǎn)回登錄頁面
// ①將異常信息存入請求域
request.setAttribute("message", e.getMessage());
// ②處理視圖:index
processTemplate("index", request, response);
}else {// 8、如果不是登錄異常則封裝為運行時異常繼續(xù)拋出
throw new RuntimeException(e);
}
}
}
}
2.5.2、注冊authServlet com.atguigu.imperial.court.servlet.module.AuthServlet authServlet /auth
2.6、EmpService 方法public class EmpServiceImpl implements EmpService {private EmpDao empDao = new EmpDaoImpl();
@Override
public Emp getEmpByLoginAccount(String loginAccount, String loginPassword) {// 1、對密碼執(zhí)行加密
String encodedLoginPassword = MD5Util.encode(loginPassword);
// 2、根據(jù)賬戶和加密密碼查詢數(shù)據(jù)庫
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、調(diào)用父類方法查詢單個對象
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入門+高深,全網(wǎng)無出其右!),僅供個人學(xué)習(xí)使用,部分內(nèi)容為本人自己見解,與尚硅谷無關(guān)。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧