当前位置:网站首页>部门项目源码分享
部门项目源码分享
2022-08-01 22:57:00 【爪哇土著、JOElib】
部门项目源码分享
代码逻辑思维导图
链接:https://pan.baidu.com/s/10Dzu46UOsp7nHmk40gIn_Q?pwd=2t6m
提取码:2t6m
–来自百度网盘超级会员V2的分享
源码分享
后期会根据AJAX和JQuery继续完善,且网站健壮性不强,会出现线程安全问题和遇到主键冲突网站跳转不了的情况,后期也会逐一完善
package bean;
import java.io.Serializable;
import java.util.Objects;
/** * @className: bean.Dept * @description: * @author: 江骏杰 * @create: 2022-07-23 20:40 */
public class Dept implements Serializable {
public static final long serialVersionUID = 100L;
private int id;
private int deptNo;
private String deptName;
private String deptLoc;
public Dept(int id, String deptLoc) {
this.id = id;
this.deptLoc = deptLoc;
}
public Dept() {
}
public Dept(int id, int deptNo, String deptName, String deptLoc) {
this.id = id;
this.deptNo = deptNo;
this.deptName = deptName;
this.deptLoc = deptLoc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDeptNo() {
return deptNo;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptLoc() {
return deptLoc;
}
public void setDeptLoc(String deptLoc) {
this.deptLoc = deptLoc;
}
@Override
public String toString() {
return "Dept{" +
"id=" + id +
", deptNo=" + deptNo +
", deptName='" + deptName + '\'' +
", deptLoc='" + deptLoc + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dept dept = (Dept) o;
return id == dept.id && deptNo == dept.deptNo && Objects.equals(deptName, dept.deptName) && Objects.equals(deptLoc, dept.deptLoc);
}
@Override
public int hashCode() {
return Objects.hash(id, deptNo, deptName, deptLoc);
}
}
package bean;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSessionBindingEvent;
import jakarta.servlet.http.HttpSessionBindingListener;
import java.io.Serializable;
/**
* @className: bean.User
* @description:
* @author: 江骏杰
* @create: 2022-07-30 17:34
*/
public class User implements HttpSessionBindingListener, Serializable {
private String username;
private String userpwd;
public User(String username, String userpwd) {
this.username = username;
this.userpwd = userpwd;
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
@Override
public void valueBound(HttpSessionBindingEvent event) {
ServletContext application = event.getSession().getServletContext();
Object count = application.getAttribute("count");
if (count != null) {
int afCount = (Integer) count;
afCount++;
application.setAttribute("count",afCount);
}else {
application.setAttribute("count",1);
}
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
ServletContext application = event.getSession().getServletContext();
int count = (Integer)application.getAttribute("count");
count--;
application.setAttribute("count", count);
}
}
package servlet;
import bean.Dept;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import util.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* @className: servlet.DeptServlet
* @description:
* @author: 江骏杰
* @create: 2022-07-23 20:37
*/
@WebServlet({"/dept/list", "/dept/detail", "/dept/modify", "/dept/add", "/dept/del"})
public class DeptServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String requestServletPath = request.getServletPath();
if ("/dept/list".equals(requestServletPath)) {
doList(request, response);
}else if ("/dept/detail".equals(requestServletPath)) {
doDetail(request, response);
}else if ("/dept/modify".equals(requestServletPath)) {
doModify(request, response);
}else if ("/dept/add".equals(requestServletPath)) {
doAdd(request, response);
}else if ("/dept/del".equals(requestServletPath)) {
doDel(request, response);
}
}
private void doDel(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
Connection conn = null;
PreparedStatement ps = null;
int update = 0;
try {
conn = DBUtil.getConnection();
String sql = "DELETE FROM dept WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,id);
update = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps);
}
if (update == 1) {
response.sendRedirect(request.getContextPath() + "/dept/list");
}
}
private void doAdd(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String deptName = request.getParameter("dept_name");
String deptNo = request.getParameter("dept_no");
String deptLoc = request.getParameter("dept_loc");
Connection conn = null;
PreparedStatement ps = null;
int update = 0;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO dept(dept_name,dept_no,dept_loc) VALUES(?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1,deptName);
ps.setString(2,deptNo);
ps.setString(3,deptLoc);
update = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps);
}
if (update == 1) {
response.sendRedirect(request.getContextPath() + "/dept/list");
}
}
private void doModify(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String id = request.getParameter("id");
String deptName = request.getParameter("dept_name");
String deptNo = request.getParameter("dept_no");
String deptLoc = request.getParameter("dept_loc");
Connection conn = null;
PreparedStatement ps = null;
int update = 0;
try {
conn = DBUtil.getConnection();
String sql = "UPDATE dept SET dept_name = ?, dept_no = ?, dept_loc = ? WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,deptName);
ps.setString(2,deptNo);
ps.setString(3,deptLoc);
ps.setString(4,id);
update = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps);
}
if (update == 1) {
response.sendRedirect(request.getContextPath() + "/dept/list");
}
}
private void doDetail(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Dept dept = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT dept_no,dept_name,dept_loc FROM dept WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,id);
rs = ps.executeQuery();
if (rs.next()) {
int deptNo = rs.getInt(1);
String deptName = rs.getString(2);
String deptLoc = rs.getString(3);
dept = new Dept(Integer.parseInt(id), deptNo, deptName, deptLoc);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps,rs);
}
request.setAttribute("deOne",dept);
request.getRequestDispatcher("/" + request.getParameter("f") + ".jsp").forward(request, response);
}
private void doList(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Dept> list = new ArrayList<>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT id,dept_loc FROM dept";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int id = rs.getInt(1);
String deptLoc = rs.getString(2);
Dept dept = new Dept(id, deptLoc);
list.add(dept);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps,rs);
}
request.setAttribute("depts",list);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
}
package servlet;
import bean.User;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
/**
* @className: servlet.ThingFilter
* @description:
* @author: 江骏杰
* @create: 2022-07-30 17:29
*/
public class ThingFilter implements Filter {
@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(false);
if (session != null && session.getAttribute("user") != null) {
chain.doFilter(request, response);
} else {
response.sendRedirect(request.getContextPath());
}
}
}
package servlet;
import bean.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import util.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @className: bean.UserServlet
* @description:
* @author: 江骏杰
* @create: 2022-07-25 18:11
*/
@WebServlet({"/user/login","/user/logout"})
public class UserServlet extends HttpServlet {
// 升级方向
// 数据表 -> 前端页面(页面流转) -> servlet完成 -> 错误页面 -> session机制 -> 安全退出
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String servletPath = request.getServletPath();
if ("/user/login".equals(servletPath)) {
doLogin(request, response);
}else if ("/user/logout".equals(servletPath)) {
doLogout(request, response);
}
}
private void doLogout(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
HttpSession session = request.getSession(false);
session.invalidate();
Cookie username = new Cookie("username", "");
Cookie userpwd = new Cookie("userpwd", "");
username.setPath(request.getContextPath());
userpwd.setPath(request.getContextPath());
username.setMaxAge(0);
userpwd.setMaxAge(0);
response.addCookie(username);
response.addCookie(userpwd);
response.sendRedirect(request.getContextPath());
}
private void doLogin(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取信息
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
// 2.连接数据库
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String path = null;
boolean login = false;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM user WHERE username = ? AND userpwd = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,userpwd);
rs = ps.executeQuery();
login = rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn,ps,rs);
}
// 判断是否登录成功
if (login) {
path = "/dept/list";
HttpSession session = request.getSession();
User user = new User(username,userpwd);
session.setAttribute("user",user);
if ("true".equals(request.getParameter("pass"))) {
Cookie username1 = new Cookie("username", username);
Cookie userpwd1 = new Cookie("userpwd", userpwd);
username1.setMaxAge(60);
userpwd1.setMaxAge(60);
username1.setPath(request.getContextPath());
userpwd1.setPath(request.getContextPath());
response.addCookie(username1);
response.addCookie(userpwd1);
}
}else {
path = "/error.jsp";
}
response.sendRedirect(request.getContextPath() + path);
}
}
package servlet;
import bean.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import util.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @className: bean.WelcomeServlet
* @description:
* @author: 江骏杰
* @create: 2022-07-25 21:49
*/
@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
String username = null;
String userpwd = null;
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
username = cookie.getValue();
}else if ("userpwd".equals(cookie.getName())) {
userpwd = cookie.getValue();
}
}
}
if (username != null && userpwd != null) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
boolean login = false;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM user WHERE username = ? AND userpwd = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, userpwd);
rs = ps.executeQuery();
login = rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
if (login) {
HttpSession session = request.getSession();
User user = new User(username,userpwd);
session.setAttribute("user",user);
response.sendRedirect(request.getContextPath() + "/dept/list");
}
}else {
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
}
package util;
import javax.sql.DataSource;
import java.sql.*;
/**
* @className: util.DBUtil
* @description:
* @author: 江骏杰
* @create: 2022-07-17 23:12
*/
public class DBUtil {
public static DataSource dataSource = null;
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://43.142.25.180:3306/dep_db","root","Hello_world_123!!~~~");
}
public static void close(Connection conn, Statement statement) {
try {
if (statement != null)
statement.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(Connection conn, Statement statement, ResultSet resultSet) {
try {
// 先关小的
if (resultSet != null)
resultSet.close();
if (statement != null)
statement.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0">
<welcome-file-list>
<welcome-file>welcome</welcome-file>
</welcome-file-list>
<filter>
<filter-name>thing</filter-name>
<filter-class>servlet.ThingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>thing</filter-name>
<url-pattern>/dept/*</url-pattern>
</filter-mapping>
</web-app>
<%--
Created by IntelliJ IDEA.
User: asus
Date: 2022/7/23
Time: 20:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加数据页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/dept/add" method="post">
部门编号:<input type="text" name="dept_no"><br>
部门名称:<input type="text" name="dept_name"><br>
部门地址:<input type="text" name="dept_loc"><br>
<input type="submit" value="确认添加">
</form>
</body>
</html>
<%@ page import="bean.Dept" %><%--
Created by IntelliJ IDEA.
User: asus
Date: 2022/7/23
Time: 20:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>详情页面</title>
</head>
<body>
<hr>
编号: ${deOne.id}
部门编号:${deOne.deptNo}
部门名字:${deOne.deptName}
部门地址:${deOne.deptLoc}
<hr>
<center><a href="${pageContext.request.contextPath}/dept/list">返回</a></center>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: asus
Date: 2022/7/25
Time: 20:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>失败页面</title>
</head>
<body>
<h1 align="center">账号或密码错误,<a href="${pageContext.request.contextPath}">请重新登录</a></h1>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: asus
Date: 2022/7/23
Time: 20:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>后台页面</title>
</head>
<body>
<H1 ALIGN="center">欢迎使用后台页面</H1>
<h2 align="center">登录</h2>
<hr>
<form action="${pageContext.request.contextPath}/user/login" method="post">
<center>用户名:<input type="text" name="username"></center><br>
<center>密码: <input type="password" name="userpwd"></center><br>
<center><input type="submit" value="登录"> 是否1分钟免登录<input type="radio" name="pass" value="true"></center>
</form>
<hr>
</body>
</html>
<%@ page import="bean.Dept" %>
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: asus
Date: 2022/7/23
Time: 20:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>列表页面</title>
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
</head>
<body>
<script type="text/javascript">
del = function (id) {
if (window.confirm("亲,真的要删除嘛,删除不可恢复哦!")) {
window.location.href = "${pageContext.request.contextPath}/dept/del?id=" + id;
}
}
</script>
<h3 align="center">欢迎${username}回来,当前在线人数:${count}</h3>
<table align="center">
<tr>
<th>记录编号</th>
<th>部门地址</th>
<th>操作</th>
</tr>
<c:forEach items="${depts}" var="dept">
<tr>
<td>${dept.id}</td>
<td>${dept.deptLoc}</td>
<td>
<a href="javascript:void(0)" οnclick="del(${dept.id})">删除数据</a>
<a href="dept/detail?id=${dept.id}&f=modify">修改数据</a>
<a href="dept/detail?id=${dept.id}&f=detail">数据详情</a>
</td>
</tr>
</c:forEach>
</table>
<center><a href="add.jsp">添加数据</a></center>
<center><a href="user/logout">安全退出</a></center>
</body>
</html>
<%@ page import="bean.Dept" %><%--
Created by IntelliJ IDEA.
User: asus
Date: 2022/7/23
Time: 20:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改页面</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/dept/modify" method="post">
<input type="text" name="id" value="${deOne.id}" hidden>
部门编号:<input type="text" name="dept_no" value="${deOne.deptNo}" readonly><br>
部门名称:<input type="text" name="dept_name" value="${deOne.deptName}"><br>
部门地址:<input type="text" name="dept_loc" value="${deOne.deptLoc}"><br>
<input type="submit" value="确认修改">
</form>
</body>
</html>
边栏推荐
- SQL Server (design database--stored procedure--trigger)
- JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
- APP专项测试:流量测试
- 企业公众号文章写作方向:如何写出读者认可的优质内容
- excel edit a cell without double clicking
- (Translation) How the contrasting color of the button guides the user's actions
- xctf攻防世界 Web高手进阶区 web2
- vscode hide menu bar
- Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
- 2022-08-01 第八组 曹雨 泛型 枚举
猜你喜欢

Delicious this year

D - Linear Probing- 并查集

小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT

【开源】Sentinel高性能高可用集群限流解决方案

联邦学习的框架搭建

From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program

From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs

y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)

域名重定向工具 —— SwitchHosts 实用教程

从0到100:招生报名小程序开发笔记
随机推荐
From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
SQL Server (design database--stored procedure--trigger)
浅析多服务在分布式系统下多事务通信处理机制方案
No more rolls!After joining ByteDance for a week, he ran decisively.
美赞臣EDI 940仓库装运订单详解
Mini Program Graduation Works WeChat Food Recipe Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
familiar friend
excel split text into different rows
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
How to use pywinauto and pyautogui to link the anime lady and sister please go home
APP专项测试:流量测试
B. Difference Array--Codeforces Round #808 (Div. 1)
Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
联邦学习入门
Quarantine and downgrade
excel edit a cell without double clicking
excel vertical to horizontal
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
D - Linear Probing- 并查集