JSP實現(xiàn)簡單人事管理系統(tǒng)
本文實例為大家分享了jsp實現(xiàn)簡單人事管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
此系統(tǒng)使用jsp實現(xiàn),其中包含了jsp九大內(nèi)置對象和四大作用域的相關(guān)知識,采用map集合模擬數(shù)據(jù)庫的方式,實現(xiàn)用戶登錄、員工信息展示、員工信息修改功能。
jsp的九大內(nèi)置對象:application,config,exception,out,pagecontent,page,request,respsonse,sesstion
jsp的四大作用域:application sesstion page request
項目結(jié)構(gòu)
emp.java 員工信息
package org.wang.model; public class emp { private string account; private string name; private string password; private string email; public emp(string account, string name, string password, string email) { this.account = account; this.name = name; this.password = password; this.email = email; } public string getaccount() { return account; } public void setaccount(string account) { this.account = account; } public string getname() { return name; } public void setname(string name) { this.name = name; } 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; } }
dbutil.java 用集合模擬數(shù)據(jù)庫存放員工信息
package org.wang.db; import org.wang.model.emp; import java.util.hashmap; import java.util.map; //用集合模擬操縱數(shù)據(jù)庫 public class dbutil{ public static map<string, emp> map = new hashmap<string, emp>(); //用靜態(tài)代碼塊完成對map中的值的初始化操作 static { map.put("001",new emp("001","zhangsan","111111","111111@qq.com")); map.put("002",new emp("002","lisi","121212","121212@qq.com")); map.put("003",new emp("003","wangwu","131313","131313@qq.com")); map.put("004",new emp("004","zhaoliu","141414","141414@qq.com")); } //判斷用戶名和密碼是否正確 public static boolean selectempbyaccountandpassword(emp emp){ //用戶輸入的信息存入到emp對象中,判斷emp對象中的值是否和map中的值對應(yīng) boolean flag = false; //遍歷當(dāng)前map集合中的key for (string key : map.keyset()){ emp e = map.get(key); //判斷用戶傳入的值是否與map集合中的值相等 if(emp.getaccount().equals(e.getaccount()) && emp.getpassword().equals(e.getpassword())){ flag = true; break; } } return flag; } }
index.jsp 登錄界面
<%-- created by intellij idea. user: wangy date: 2018/11/8 time: 8:19 to change this template use file | settings | file templates. --%> <%@ page contenttype="text/html;charset=utf-8" language="java" %> <html> <head> <title>人事管理系統(tǒng)登錄</title> </head> <body> <h3 align="center">人事管理系統(tǒng)登錄頁面</h3> <hr> <%--action代表了服務(wù)器端的處理程序--%> <form action="index-control.jsp"> <table align="center"> <tr> <td> 賬號: </td> <td> <input type="text" name="account"> </td> </tr> <tr> <td> 密碼: </td> <td> <input type="password" name="password"> </td> </tr> <tr> <td> <input type="submit" value="登錄"> </td> </tr> </table> </form> </body> </html>
index-control.jsp 登錄界面的控制界面,用于處理用戶登錄信息是否與map集合中的員工信息匹配
<%-- created by intellij idea. user: wangy date: 2018/11/8 time: 9:09 to change this template use file | settings | file templates. --%> <%@ page contenttype="text/html;charset=utf-8" language="java" errorpage="error.jsp" %> <%@ page import="org.wang.db.*,org.wang.model.*" %> <%@ page import="java.util.map" %> <html> <head> <title>人事管理系統(tǒng)</title> </head> <body> <%--獲取用戶輸入的賬號及密碼,并調(diào)用dbutil中的方法判斷信息是否存在 request:獲取請求信息 request.getparameter(string name):可以通過一個控件的name屬性來獲取控件的值 out.println(); 向頁面輸出信息 --%> <% // 獲取用戶輸入的賬號及密碼 string account = request.getparameter("account"); string password = request.getparameter("password"); //將用戶輸入的賬號和密碼封裝到一個emp對象中 emp emp = new emp(account,null,password,null); boolean flag = dbutil.selectempbyaccountandpassword(emp); //獲取map集合 map<string,emp> map = dbutil.map; if(flag==true){ //設(shè)置session session.setattribute("account",account); //使用application來獲取系統(tǒng)訪問量 object o = application.getattribute("count"); //判斷如果當(dāng)前用戶為第一個登錄,則application中的值為空,此時將訪問量設(shè)置為1 if(o == null){ application.setattribute("count",1); }else{ //count原來為string,強轉(zhuǎn)為int型,并做+1操作 int count = integer.parseint(o.tostring()); application.setattribute("count",count+1); } %> <%--獲取訪問量并顯示到頁面上--%> <h3 align="right">當(dāng)前訪問量:<%=application.getattribute("count")%></h3> <%--獲取session中的值并顯示到頁面上--%> <h3 align="center">歡迎來到人事管理系統(tǒng)</h3> <h3 align="right">登錄賬戶:<%=session.getattribute("account")%></h3> <hr> <table align="center" border="1" width="500px"> <tr> <td> 賬號 </td> <td> 員工姓名 </td> <td> 郵箱 </td> <td> 修改 </td> </tr> <%--用for循環(huán)自動根據(jù)模擬數(shù)據(jù)庫中的數(shù)據(jù)生成單元行,顯示出員工信息表--%> <% for (string key : map.keyset()){ emp e = map.get(key); %> <tr> <td> <%=e.getaccount()%> </td> <td> <%=e.getname()%> </td> <td> <%=e.getemail()%> </td> <td> <%--點擊修改跳轉(zhuǎn)到update.jsp頁面,采用url方式傳遞參數(shù),地址欄會顯示數(shù)據(jù)信息--%> <%--相鄰兩個jsp頁面?zhèn)鬟f數(shù)據(jù)時,可通過url參數(shù)的方式傳遞--%> <%--語法規(guī)則:頁面?key1=value1 & key2=value2--%> <a href="update.jsp?account=<%=e.getaccount()%>&name=<%=e.getname()%>&email=<%=e.getemail()%>" rel="external nofollow" >修改</a> </td> </tr> <% } %> </table> <% }else{ throw new exception("登錄失敗"); } %> </body> </html>
error.jsp
<%-- created by intellij idea. user: wangy date: 2018/11/8 time: 16:01 to change this template use file | settings | file templates. --%> <%@ page contenttype="text/html;charset=utf-8" language="java" iserrorpage="true" %> <html> <head> <title>title</title> </head> <body> <%=exception.getmessage()%> </body> </html>
update.jsp 修改員工信息頁面
%-- created by intellij idea. user: wangy date: 2018/11/8 time: 15:27 to change this template use file | settings | file templates. --%> <%@ page contenttype="text/html;charset=utf-8" language="java" %> <html> <head> <title>員工更新頁面</title> </head> <body> <h3 align="right">當(dāng)前訪問量:<%=application.getattribute("count")%></h3> <h3 align="center">員工更新頁面</h3> <%--獲取session中的值并顯示到頁面上--%> <h3 align="right">登錄賬戶:<%=session.getattribute("account")%></h3> <hr> <form action="update-control.jsp"> <table align="center" border="1" width="500px"> <tr> <%--value="<%=request.getparameter("account")%>"可用于實現(xiàn)數(shù)據(jù)的回顯--%> <td>賬號</td> <td><input type="text" name="account" value="<%=request.getparameter("account")%>"></td> </tr> <tr> <td>姓名</td> <td><input type="text" name="name" value="<%=request.getparameter("name")%>"></td> </tr> <tr> <td>郵箱</td> <td><input type="text" name="email" value="<%=request.getparameter("email")%>"></td> </tr> <tr> <td> <input type="submit" value="修改"> </td> </tr> </table> </form> </body> </html>
update-control 執(zhí)行修改操作的控制頁面
<%-- created by intellij idea. user: wangy date: 2018/11/9 time: 9:46 to change this template use file | settings | file templates. --%> <%@ page contenttype="text/html;charset=utf-8" language="java" %> <%@page import="org.wang.db.*,org.wang.model.*" %> <%@ page import="java.util.map" %> <html> <head> <title>title</title> </head> <body> <% //獲取map集合 map<string,emp> map = dbutil.map; //修改信息 //獲取當(dāng)前需要修改的員工的account emp emp = map.get(request.getparameter("account")); //把獲取到的當(dāng)前員工的信息重新set emp.setname(request.getparameter("name")); emp.setemail(request.getparameter("email")); %> <h3 align="center">修改員工信息成功</h3> </body> </html>
運行效果
登錄界面
登錄成功后進入員工信息顯示頁面
修改員工信息(這里用了數(shù)據(jù)回顯)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。
相關(guān)文章
- jsp+servlet實現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計的實現(xiàn)方法
- 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
- 詳解JSP 內(nèi)置對象request常見用法
- 使用IDEA編寫jsp時EL表達(dá)式不起作用的問題及解決方法
- jsp實現(xiàn)局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎(chǔ)實踐
- JavaServlet的文件上傳和下載實現(xiàn)方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法