当前位置:网站首页>Department project source code sharing
Department project source code sharing
2022-08-01 23:11:00 【Native Javanese, JOElib】
Department project source code sharing
Code logic mind map
链接:https://pan.baidu.com/s/10Dzu46UOsp7nHmk40gIn_Q?pwd=2t6m
提取码:2t6m
–来自百度网盘超级会员V2的分享
源码分享
后期会根据AJAX和JQuery继续完善,And the site's robustness is not strong,There will be thread safety problems and the situation that the website cannot be jumped when encountering a primary key conflict,It will be improved one by one later
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">Welcome to the backend page</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="登录"> 是否1Free login in minutes<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("亲,Really want to delete it,Deletion is irreversible!")) {
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>
边栏推荐
- chrome copies the base64 data of an image
- chrome复制一张图片的base64数据
- 部门项目源码分享
- Chapter 11 Working with Dates and Times
- 毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?
- 浅析多服务在分布式系统下多事务通信处理机制方案
- Quarantine and downgrade
- qt-faststart installation and use
- How to better understand and do a good job?
- y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
猜你喜欢
Deep Learning Course2 Week 2 Optimization Algorithms Exercises
解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
数据分析04
TCP 可靠吗?为什么?
13、学习MySQL 分组
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
访问控制台中的选定节点
解决端口占用
随机推荐
PDF转Word有那么难吗?做一个文件转换器,都解决了
How to better understand and do a good job?
系统可用性:SRE口中的3个9,4个9...到底是个什么东西?
从0到100:招生报名小程序开发笔记
How do programmers solve online problems gracefully?
美赞臣EDI 940仓库装运订单详解
C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
SRv6 L3VPN的工作原理
excel split text into different rows
A. Doremy‘s IQ-- Codeforces Round #808 (Div. 1)
简单3D渲染器的制作
From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
[Recommended books] The first self-driving technology book
最短路模板
xctf attack and defense world web master advanced area web2
域名重定向工具 —— SwitchHosts 实用教程
Oracle 数据库设置为只读及读写
Is TCP reliable?Why?
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
解决端口占用