人事管理系统案例
人事管理系统为你解剖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,好,完成这些,就可以开始敲代码了。
实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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() { } } |
模拟数据库类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <%@ 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | <%@ 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的页面,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <%@ page contentType= "text/html;charset=UTF-8" language= "java" isErrorPage= "true" %> <html> <head> <title>报错页面</title> </head> <body> <%=exception.getMessage()%> </body> </html> |
在此指定为错误页面。
JSP之修改页面(update.jsp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <%@ 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | <%@ 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匹马行天下”和学习心得分享“匹马行天下”,在分享中进步,越努力越幸运,期待我们都有美好的明天!
支持我的朋友们记得点波推荐哦,您的肯定就是我进步的动力。

作者:泰斗贤若如
微信公众号:去有风的地方飞翔
Github:https://github.com/zyx110
有事微信:zyxt1637039050
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

我不能保证我所说的都是对的,但我能保证每一篇都是用心去写的,我始终认同: “分享的越多,你的价值增值越大”,我们一同在分享中进步,在分享中成长,越努力越幸运。再分享一句话“十年前你是谁,一年前你是谁,甚至昨天你是谁,都不重要。重要的是,今天你是谁,以及明天你将成为谁。”
人生赢在转折处,改变从现在开始!
支持我的朋友们记得点波推荐哦,您的肯定就是我前进的动力。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?