接上次的版本,把上次的修改成為基于web的專案.
總算是可以脫離命令列,使用jsp作為前端,能有一個友好的頁面了.
上次既然已經開始用三層架構的模式了,這次也就接著使用三層架構,把之前的拿過來繼續改改用.
首先核心要修改的就是controller層,因為要改成web專案,controller層負責的就是用戶互動,收集用戶輸入的資訊,展示資訊,而這些東西都要交給servlet和jsp完成.
又因為不適合在jsp中撰寫復雜邏輯,所以就直接用servlet來充當controller層.
service層和dao層暫時先不用修改.瞬間體會到了三層架構的優勢,每層都各司其職,修改只需要修改相關的層,不相關的完全可以不用修改.
既然用了web,就單純的想給這個系統添加一個用戶登錄的功能,以此練習一下用戶登錄相關的功能.
index.jsp就簡簡單單的給個超鏈接指向登錄頁面login.jsp
<a href="https://www.cnblogs.com/yao-xi/archive/2020/11/19/${pageContext.request.contextPath}/login.jsp">去登錄</a>
其中通過EL運算式${pageContext.request.contextPath}動態獲取虛擬路徑,之后拼接login.jsp的路徑即可跳轉過去.
在login.jsp中也就一個簡單的表單,用來做登錄資訊的提交.
<form action="${pageContext.request.contextPath}/LoginServlet" method="post">
<div>
<label for="username">用戶名</label>
<input type="text" name="username" id="username"/>
</div>
<div>
<label for="password">密碼</label>
<input type="password" name="password" id="password"/>
</div>
<div>
<input type="submit" value="https://www.cnblogs.com/yao-xi/archive/2020/11/19/提交">
</div>
</form>
表單肯定是直接提交到servlet中的,所以action直接指向LoginServlet,當然這個路徑也通過EL運算式拼接絕對路徑.
LoginServlet需要繼承HttpServlet.并且重寫doPost和doGet方法.Tomcat會在用戶訪問的時候,自動呼叫HttpServlet中的service
方法的,而在HttpServlet的service方法中,又根據請求的資訊,分別呼叫doPost和doGet方法.
在LoginServlet中,主要要做的事便是
- 接收請求資料
- 打包資料
- 扔給service層處理業務
- 接收service層的回傳結果
- 根據結果判斷應該怎么做
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 接收請求的資料
Map<String, String[]> map = req.getParameterMap();
// 使用BeanUtils封裝資料
User user = new User();
try {
BeanUtils.populate(user, map);
} catch (Exception e) {
e.printStackTrace();
}
// 將物件交給service并處理業務
UserService service = new UserService();
User loginUser = service.login(user);
if (loginUser != null) {
// 登錄成功 把user的資訊在會話共享
req.getSession().setAttribute("user", loginUser);
// 重定向到首頁
resp.sendRedirect(req.getContextPath()+"/index.jsp");
}else{
// 登錄失敗 轉發并回傳提示資訊
req.setAttribute("msg", "用戶名或密碼錯誤");
req.getRequestDispatcher("/login.jsp").forward(req,resp);
}
}
既然要回傳失敗資訊,那么就給login.jsp頁面添加一個div用來展示回傳的資訊.
<%-- 登錄錯誤提示資訊顯示 --%>
<div style="color: red;font-weight: bold">
${requestScope.msg}
</div>
這里使用EL運算式,獲取請求域中的msg屬性,因為在Servlet中,把錯誤訊息就塞在msg屬性中.
既然有了登錄功能,那就把index.jsp也修改一下,增加一個登錄后的頁面變化.
<%--未登錄則需要登錄,登陸后看到功能頁面--%>
<c:if test="${empty sessionScope.user}">
<a href="https://www.cnblogs.com/yao-xi/archive/2020/11/19/${pageContext.request.contextPath}/login.jsp">去登錄</a><br>
</c:if>
<c:if test="${not empty sessionScope.user}">
<div>歡迎 ${sessionScope.user.username}</div>
<div>功能串列</div>
<div>
<a href="https://www.cnblogs.com/yao-xi/archive/2020/11/19/${pageContext.request.contextPath}/ShowAllStudentServlet">查看學生</a>
</div>
<div>
<a href="https://www.cnblogs.com/yao-xi/archive/2020/11/19/${pageContext.request.contextPath}/ExitServlet">退出登錄</a>
</div>
</c:if>
這里登錄頁面的變化使用了jstl的if標簽,當test屬性值為true時,顯示if標簽內的元素.
至此,用戶登錄的controller層基本上搞定了.service,dao這兩層和學生管理的部分差不多,就略掉不寫在這了.其中的User類也就僅僅包含id,username,password三個屬性.
后續待更,學生管理部分的Servlet有點多,慢慢更吧.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/225038.html
標籤:其他
