最终简易oa项目代码

最终简易oa项目代码

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
package com.xiaoguan.oa.dept.bean;

import java.util.Objects;

public class Dept {
private String deptno;
private String dname;
private String loc;

public Dept(String deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}

public Dept() {
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dept dept = (Dept) o;
return Objects.equals(getDeptno(), dept.getDeptno()) && Objects.equals(getDname(), dept.getDname()) && Objects.equals(getLoc(), dept.getLoc());
}

@Override
public int hashCode() {
return Objects.hash(getDeptno(), getDname(), getLoc());
}

@Override
public String toString() {
return "Dept{" +
"deptno='" + deptno + '\'' +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}

public String getDeptno() {
return deptno;
}

public void setDeptno(String deptno) {
this.deptno = deptno;
}

public String getDname() {
return dname;
}

public void setDname(String dname) {
this.dname = dname;
}

public String getLoc() {
return loc;
}

public void setLoc(String loc) {
this.loc = loc;
}
}

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
package com.xiaoguan.oa.dept.bean;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSessionAttributeListener;
import jakarta.servlet.http.HttpSessionBindingEvent;
import jakarta.servlet.http.HttpSessionBindingListener;

import java.util.Objects;

public class User implements HttpSessionBindingListener {
@Override
public void valueBound(HttpSessionBindingEvent event) {
ServletContext application = event.getSession().getServletContext();
Object onlineCount = application.getAttribute("onlineCount");
if (onlineCount == null) {
application.setAttribute("onlineCount",1);
}else {
int count=(Integer) onlineCount;
count++;
application.setAttribute("onlineCount",count);
}
}

@Override
public void valueUnbound(HttpSessionBindingEvent event) {
ServletContext application = event.getSession().getServletContext();
Object onlineCount = application.getAttribute("onlineCount");
int count=(Integer) onlineCount;
count--;
application.setAttribute("onlineCount",count);
}

private String username;
private String password;

public User(String username, String password) {
this.username = username;
this.password = password;
}

public User() {
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(getUsername(), user.getUsername()) && Objects.equals(getPassword(), user.getPassword());
}

@Override
public int hashCode() {
return Objects.hash(getUsername(), getPassword());
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

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
package com.xiaoguan.oa.dept.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
HttpSession session = request.getSession();
String servletPath = request.getServletPath();
boolean whiteList="/welcome".equals(servletPath)||"/login".equals(servletPath)||
"/logout".equals(servletPath)||"/freeLogin".equals(servletPath)||
"/index.jsp".equals(servletPath)||"/error.jsp".equals(servletPath);
if(whiteList||(session!=null&&session.getAttribute("id")!=null)){
chain.doFilter(request,response);

} else{
response.sendRedirect(request.getContextPath()+"/welcome");
}
}

@Override
public void destroy() {

}
}

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
package com.xiaoguan.oa.dept.utils;


import java.sql.*;
import java.util.ResourceBundle;

public class DBUtil {
private static ResourceBundle bundle=ResourceBundle.getBundle("resources.jdbc");
private static String driver=bundle.getString("driver");
private static String url=bundle.getString("url");
private static String username=bundle.getString("username");
private static String password=bundle.getString("password");
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
public static void close(Connection conn, Statement ps,ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.xiaoguan.oa.dept.web;

import com.xiaoguan.oa.dept.bean.Dept;
import com.xiaoguan.oa.dept.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/list","/detail","/delete","/add","/modify"})
public class DeptServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String servletPath = request.getServletPath();
if("/list".equals(servletPath)){
doList(request,response);
} else if ("/detail".equals(servletPath)) {
doDetail(request,response);
} else if ("/delete".equals(servletPath)) {
doDel(request,response);
} else if ("/add".equals(servletPath)) {
doAdd(request,response);

} else if ("/modify".equals(servletPath)) {
doModify(request,response);
} else {
System.out.println("路径有误!");
}
}

private void doModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String deptno = request.getParameter("deptno");
String dname = request.getParameter("dname");
String loc = request.getParameter("loc");
Connection conn=null;
PreparedStatement ps=null;
int count=0;
try {
conn= DBUtil.getConnection();
String sql="update dept set dname=?,loc=? where deptno=?";
ps=conn.prepareStatement(sql);
ps.setString(3,deptno);
ps.setString(1,dname);
ps.setString(2,loc);
count=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,null);
}
if(count==1){
response.sendRedirect(request.getContextPath()+"/list");
}else {
response.sendRedirect(request.getContextPath()+"/error.jsp");
}
}

private void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String deptno = request.getParameter("deptno");
String dname = request.getParameter("dname");
String loc = request.getParameter("loc");
Connection conn=null;
PreparedStatement ps=null;
int count=0;
try {
conn= DBUtil.getConnection();
String sql="insert into dept(deptno,dname,loc) values(?,?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1,deptno);
ps.setString(2,dname);
ps.setString(3,loc);
count=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,null);
}
if(count==1){
response.sendRedirect(request.getContextPath()+"/list");
}else {
response.sendRedirect(request.getContextPath()+"/error.jsp");
}
}

private void doDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String deptno = request.getParameter("deptno");
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
int count=0;
try {
conn=DBUtil.getConnection();
String sql="delete from dept where deptno=?";
ps=conn.prepareStatement(sql);
ps.setString(1,deptno);
count=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
if(count==1){
response.sendRedirect(request.getContextPath()+"/list");
}else {
response.sendRedirect(request.getContextPath()+"/error.jsp");
}
}

private void doDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String deptno = request.getParameter("deptno");
String flag = request.getParameter("f");
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
Dept dept=null;
try {
conn=DBUtil.getConnection();
String sql="select dname,loc from dept where deptno=?";
ps=conn.prepareStatement(sql);
ps.setString(1,deptno);
rs=ps.executeQuery();
if(rs.next()){
String dname = rs.getString("dname");
String loc = rs.getString("loc");
dept=new Dept(deptno,dname,loc);
request.setAttribute("dept",dept);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
if("d".equals(flag)){
request.getRequestDispatcher("/detail.jsp").forward(request,response);
} else if ("m".equals(flag)) {
request.getRequestDispatcher("/edit.jsp").forward(request,response);
}else {
System.out.println("f传参参数有误!");
}

}

private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Dept> depts = new ArrayList<>();
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn=DBUtil.getConnection();
String sql="select deptno,dname,loc from dept";
ps= conn.prepareStatement(sql);
rs=ps.executeQuery();
while (rs.next()) {
String deptno=rs.getString("deptno");
String dname = rs.getString("dname");
String loc=rs.getString("loc");
Dept dept=new Dept(deptno,dname,loc);
depts.add(dept);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
request.setAttribute("deptList",depts);
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
}

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
package com.xiaoguan.oa.dept.web;

import com.xiaoguan.oa.dept.bean.User;
import com.xiaoguan.oa.dept.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


@WebServlet({"/login","/logout","/freeLogin"})
public class UserServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String servletPath = request.getServletPath();
if("/login".equals(servletPath)){
doLogin(request,response);
} else if ("/logout".equals(servletPath)) {
doLogout(request,response);
} else if ("/freeLogin".equals(servletPath)) {
doFreeLogin(request,response);
} else{
System.out.println("用户Servlet操作错误");
}
}

private void doFreeLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie userNameLoginCookie = new Cookie("userName", "");
Cookie userPasswordLoginCookie = new Cookie("userPassword", "");
userNameLoginCookie.setMaxAge(0);
userPasswordLoginCookie.setMaxAge(0);
userNameLoginCookie.setPath(request.getContextPath());
userPasswordLoginCookie.setPath(request.getContextPath());
response.addCookie(userNameLoginCookie);
response.addCookie(userPasswordLoginCookie);
response.sendRedirect(request.getContextPath()+"/list");
}

private void doLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
if(session!=null){
session.removeAttribute("user");
session.invalidate();
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}

protected void doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
String userPassword = request.getParameter("userPassword");
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean flag=false;
int id=0;
try {
conn= DBUtil.getConnection();
String sql="select id from login where 登录名=? and 登录密码=?";
ps=conn.prepareStatement(sql);
ps.setString(1,userName);
ps.setString(2,userPassword);
rs=ps.executeQuery();
if(rs.next()){
flag=true;
id=rs.getInt("id");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
if(flag){
HttpSession session = request.getSession();
User user=new User(userName,userPassword);
session.setAttribute("user",user);
System.out.println("放user了");
session.setAttribute("id",id);
session.setAttribute("userName",userName);
if("1".equals(request.getParameter("flag")))
{
Cookie userNameLoginCookie = new Cookie("userName", userName);
Cookie userPasswordLoginCookie = new Cookie("userPassword", userPassword);
userNameLoginCookie.setMaxAge(60*60*24*10);
userPasswordLoginCookie.setMaxAge(60*60*24*10);
userNameLoginCookie.setPath(request.getContextPath());
userPasswordLoginCookie.setPath(request.getContextPath());
response.addCookie(userNameLoginCookie);
response.addCookie(userPasswordLoginCookie);
}
response.sendRedirect(request.getContextPath()+"/list");
}else{
response.sendRedirect(request.getContextPath()+"/error.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
package com.xiaoguan.oa.dept.web;

import com.xiaoguan.oa.dept.bean.User;
import com.xiaoguan.oa.dept.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
String userPassword=null;
String userName=null;
if (cookies != null) {
for (Cookie cookie : cookies) {
String name = cookie.getName();
if("userName".equals(name)){
userName = cookie.getValue();
}
if("userPassword".equals(name)){
userPassword = cookie.getValue();
}
}
}
if(userName!=null&&userPassword!=null){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean flag=false;
int id=0;
try {
conn= DBUtil.getConnection();
String sql="select id from login where 登录名=? and 登录密码=?";
ps=conn.prepareStatement(sql);
ps.setString(1,userName);
ps.setString(2,userPassword);
rs=ps.executeQuery();
if(rs.next()){
flag=true;
id=rs.getInt("id");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
if(flag) {
HttpSession session = request.getSession();
User user=new User(userName,userPassword);
session.setAttribute("user",user);
session.setAttribute("id", id);
session.setAttribute("userName", userName);
response.sendRedirect(request.getContextPath() + "/list");
}else {
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}else{
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}
}

1
2
3
4
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/xiaoguan
username=root
password=密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>新增部门</title>
</head>
<body>
<h1>新增部门</h1>
<hr>
<form action="${pageContext.request.contextPath}/add" method="post">
部门编号<input type="text" name="deptno"/><br>
部门名称<input type="text" name="dname"/><br>
部门位置<input type="text" name="loc"/><br>
<input type="submit" value="保存">
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>部门详情</title>
</head>
<body>
<h1>部门详情</h1>
<hr>
部门编号<input type="text" value="${dept.deptno}" readonly/><br>
部门名称<input type="text" value="${dept.dname}" readonly/><br>
部门位置<input type="text" value="${dept.loc}" readonly/><br>
<input type="button" value="后退" onclick="window.history.back()">
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>修改部门</title>
</head>
<body>
<h1>修改部门</h1>
<hr>
<form action="${pageContext.request.contextPath}/modify" method="post">
部门编号<input type="text" name="deptno" value="${dept.deptno}" readonly/><br>
部门名称<input type="text" name="dname" value="${dept.dname}"/><br>
部门位置<input type="text" name="loc" value="${dept.loc}"/><br>
<input type="submit" value="修改">
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>操作或登录失败</title>
</head>
<body>
<h1><a href="javascript:void(0)" onclick="document.location.href='${pageContext.request.contextPath}/welcome'">操作或登录失败请返回起始页</a></h1>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>欢迎使用OA系统</title>
</head>
<body>
<%--<a href="<%=request.getContextPath()%>/list">点击进入部门列表</a><br>--%>
<h1>用户登录</h1>
<hr>
<form action="${pageContext.request.contextPath}/login" method="post">
用户名:<input type="text" name="userName"/><br>
密码:<input type="password" name="userPassword"><br>
<input type="checkbox" name="flag" value="1">十天内免登录<br>
<input type="submit" value="登录">
</form>

</body>
</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
43
44
45
46
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>部门列表</title>
<script>
function del(deptno){
if(window.confirm('请确认是否删除!')){
document.location.href='${pageContext.request.contextPath}/delete?deptno='+deptno;
}
}
</script>
</head>
<body>
<h3>欢迎${userName},在线人数为${onlineCount}</h3><br>
<a href="${pageContext.request.contextPath}/logout">[退出系统]</a>
<a href="${pageContext.request.contextPath}/freeLogin">[退出免登陆状态]</a>
<h1 align="center">部门列表</h1>
<hr>
<table border="1px" align="center" width="50%">
<tr>
<th>序号</th>
<th>部门编号</th>
<th>部门名称</th>
<th>操作</th>
</tr>
<c:forEach items="${deptList}" varStatus="varStatus" var="dept">
<tr>
<td>${varStatus.count}</td>
<td>${dept.deptno}</td>
<td>${dept.dname}</td>
<td>
<a href="javascript:void(0)" onclick="del(${dept.deptno})">删除</a>
<a href="${pageContext.request.contextPath}/detail?f=m&deptno=${dept.deptno}">修改</a>
<a href="${pageContext.request.contextPath}/detail?f=d&deptno=${dept.deptno}">详情</a>
</td>
</tr>
</c:forEach>
</table>
<hr>
<a href="${pageContext.request.contextPath}/add.jsp">新增部门</a><br>
<a href="${pageContext.request.contextPath}/index.jsp">回到起始页</a>
</body>
</html>