人事管理系統為你解剖JSP
前言:
之前寫過兩篇學習JSP的博客,《Java匹馬行天下之JavaWeb核心技術——JSP》https://www.cnblogs.com/zyx110/p/10926587.html ,里面詳細解說了學習JSP需要的所有知識點。這幾天要給身邊的兩個朋友講JSP,翻着看了看之前寫的博客,知識雖然很全,但太多了,如果是新手,看着會很枯燥,那個只適合學過一遍后的人回頭復習的時候查閱,不適合初學者入門學習,為此,我特意找了一篇人事管理系統案例,通過案例去理解和學習JSP,希望能對一些需要的朋友有所幫助。
案例介紹:
此篇用純JSP技術,實現了一個完整且簡單的人事管理系統,用Map集合模擬數據庫的數據存儲,有登錄,頁面跳轉,Session存儲,修改等知識的應用,我覺得對於初學者,這是再適合不過的案例了,特作此篇,友情奉獻,如有欠缺,忘海涵並指正。
案例演示:
案例講解
以上演示的只是其中的一部分,里面有一些細節,我會在后面講解的時候細說。
案例結構及案例准備
我用的開發工具是IDEA,如果有不會用IDEA的朋友可以看之前寫過的博客《IDEA新手使用教程》https://www.cnblogs.com/zyx110/p/10666082.html,我建的這是一個Maven項目,如果有朋友不知道Maven,可以先看一下我之前寫的介紹Maven的博客《Maven》https://www.cnblogs.com/zyx110/p/10619148.html,不知道如何配置Maven環境的可以看《Maven的安裝與配置》https://www.cnblogs.com/zyx110/p/10801666.html不知道如何在IDEA中建Maven項目的朋友可以看《IDEA為新手專業打造》https://www.cnblogs.com/zyx110/p/10802098.html,此案例還會用到Tomcat,同樣,不會在IDEA中配置Tomcat的朋友可以看《IDEA為新手專業打造》https://www.cnblogs.com/zyx110/p/10802098.html,好,完成這些,就可以開始敲代碼了。
實體類
package entity; public class Emp { private String account;//賬號 private String password;//密碼 private String email;//郵箱 public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Emp(String account, String password, String email) { this.account = account; this.password = password; this.email = email; } public Emp() { } }
模擬數據庫類
package db; import entity.Emp; import java.util.HashMap; import java.util.Map; public class DBUtil { public static Map<String, Emp> map = new HashMap<String, Emp>(); static { map.put("101", new Emp("101", "123456", "101@qq.com")); map.put("102", new Emp("102", "123456", "102@qq.com")); map.put("103", new Emp("103", "123456", "103@qq.com")); map.put("104", new Emp("104", "123456", "104@qq.com")); } public static boolean isAccountAndPassword(Emp emp) { boolean flag = false; for (String key : map.keySet()) { Emp emp1 = map.get(key); if (emp1.getAccount().equals(emp.getAccount()) && emp1.getPassword().equals(emp.getPassword())) { flag = true; break; } } return flag; } }
JSP之登錄頁面(login.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登錄頁面</title> </head> <body> <h3 align="center">人事管理系統</h3> <form action="controller.jsp"> <table align="center" border="1px" bgcolor="#ff8c00" width="500px" height="300px"> <tr> <td align="center">賬號:</td> <td align="center"><input type="text" name="account"></td> </tr> <tr> <td align="center">密碼:</td> <td align="center"><input type="password" name="password"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="登錄"></td> </tr> </table> </form> </body> </html>
JSP之登錄判斷及數據顯示頁面(controller.jsp)
<%@ page import="entity.Emp" %> <%@ page import="db.DBUtil" %> <%@ page import="java.util.Map" %> <%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %> <html> <head> <title>人員判斷和顯示頁面</title> </head> <body> <% //接收登錄頁面請求的參數 String account = request.getParameter("account"); String password = request.getParameter("password"); Emp emp = new Emp(account,password,null); //判斷人員信息 boolean flag = DBUtil.isAccountAndPassword(emp); Map<String,Emp> map = DBUtil.map; if (flag){ session.setAttribute("account",account); Object o =application.getAttribute("count"); if (o==null){ application.setAttribute("count",1); }else { int count = Integer.parseInt(o.toString()); application.setAttribute("count",count+1); } %> <h3 align="right">訪問量:<%=application.getAttribute("count")%></h3> <h3 align="right">當前賬戶:<%=session.getAttribute("account")%></h3> <table align="center" border="1px" bgcolor="#f5f5dc" width="500px" height="500px"> <tr> <td align="center">賬戶</td> <td align="center">密碼</td> <td align="center">郵箱</td> <td align="center">修改</td> </tr> <%for (String key:map.keySet()){ Emp emp1 = map.get(key); %> <tr> <td align="center"><%=emp1.getAccount()%></td> <td align="center"><%=emp1.getPassword()%></td> <td align="center"><%=emp1.getEmail()%></td> <td align="center"><a href="update.jsp?account=<%=emp1.getAccount()%>&password=<%=emp1.getPassword()%>&email=<%=emp1.getEmail()%>">修改</a></td> </tr> <% } }else { throw new Exception("賬號或密碼錯誤"); } %> </table> </body> </html>
內容解析:
如圖所示:
1、Session:主要用於跟蹤會話
什么是會話?
會話是代表用戶第一次進入當前系統直到退出系統或關閉瀏覽器,在此期間與服務器的一系列交互。
Session作用域:會話期間
在這是在session對象中存儲一些數據,實現信息共享。
2、application對象應用
Application: 提供了關於服務器版本,應用級初始化參數和應用內資源絕對路徑方式。是ServletContext類的實例,與應用上下文有關。
Application作用域:web容器的生命周期。
在這用來獲取當前系統的訪問量。
3、exception:異常對象
在JSP中如果一個頁面中出現了錯誤,可以交由另外一個頁面處理。在此頁面中指定一個錯誤處理的頁面errorPage=”error.jsp”,然后新建一個error.jsp的頁面,如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> <html> <head> <title>報錯頁面</title> </head> <body> <%=exception.getMessage()%> </body> </html>
在此指定為錯誤頁面。
JSP之修改頁面(update.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改人員信息頁面</title> </head> <body> <h3 align="right">當前賬戶:<%=session.getAttribute("account")%></h3> <form action="update_controller.jsp"> <table align="center" border="1px" bgcolor="#ff8c00"> <tr> <td>賬號</td> <td><input type="text" name="account" value="<%=request.getParameter("account")%>"></td> </tr> <tr> <td>密碼</td> <td><input type="text" name="password" value="<%=request.getParameter("password")%>"></td> </tr> <tr> <td>郵箱</td> <td><input type="text" name="email" value="<%=request.getParameter("email")%>"></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"></td> </tr> </table> </form> </body> </html>
JSP之修改成功並顯示頁面(update_controller.jsp)
<%@ page import="java.util.Map" %> <%@ page import="entity.Emp" %> <%@ page import="db.DBUtil" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改控制頁面</title> </head> <body> <% Map<String, Emp> map = DBUtil.map; Emp emp = map.get(request.getParameter("account")); emp.setPassword(request.getParameter("password")); emp.setEmail(request.getParameter("email")); %> <h3 align="right">當前賬戶:<%=session.getAttribute("account")%></h3> <h3 align="center">信息修改成功</h3> <form action=""> <table align="center" border="1px" bgcolor="#ff8c00" width="500px" height="500px"> <tr> <td>賬戶</td> <td>密碼</td> <td>郵箱</td> <td>修改</td> </tr> <% Map<String,Emp> map1 = DBUtil.map; for (String key:map1.keySet()){ Emp emp1 = map1.get(key); %> <tr> <td><%=emp1.getAccount()%></td> <td><%=emp1.getPassword()%></td> <td><%=emp1.getEmail()%></td> <td><a href="update.jsp?account=<%=emp1.getAccount()%>&password=<%=emp1.getPassword()%>&email=<%=emp1.getEmail()%>">修改</a></td> </tr> <%}%> </table> </form> </body> </html>
案例總結
其實總結這篇博客,雖然案例看着很簡單,但我覺得這里面透露着一種學習方法,通過案例去學習知識,有思維有邏輯,適合的才是最好的,避免你在學習迷茫的過程中亂沖亂撞,做一些無用功。到此案例結束,如果想系統學習JSP,就去我的博客園看《Java匹馬行天下之JavaWeb核心技術——JSP》,“https://www.cnblogs.com/zyx110/p/10926587.html” 更多精彩等你學習,記住,“越懂得分享,你的價值增值越大”。
*****************************************************************************************************
我的博客園地址:https://www.cnblogs.com/zyx110/
轉載請說明出處
我不能保證我所說的都是對的,但我能保證每一篇都是用心去寫的,我始終認同“分享的越多,你的價值增值越大”,歡迎大家關注我的技術分享“Java匹馬行天下”和學習心得分享“匹馬行天下”,在分享中進步,越努力越幸運,期待我們都有美好的明天!
支持我的朋友們記得點波推薦哦,您的肯定就是我進步的動力。