Java Web 学生上课签到打卡系统
基于Jsp+Servlet的上课考勤管理系统
实现了学生上课签到、打卡的功能。教师能够看到没有及时打卡的学生情况。
包括对签到打卡的增删改查、对学生信息的增删改查。
主要技术:Jsp+Servlet+Mysql+Tomcat+Eclipse
适用场合:Java课程设计/Java大作业
项目类型:JAVA WEB项目
用户类型:2个角色(教师+学生)
开发工具:Eclipse
数据库表:3张
介绍主要是教师在发布打卡要求以后,学生需要在规定时间内及时打卡,如果超过规定时间没有打卡,则会被记录下来。此外,还实现了对学生信息的增删改查。
1.学生 注册
2.教师/学生登录
3.添加学生
4.删除学生
5.修改学生
6.查看学生
7.发起打卡
8.查看未打卡的人
9.学生打卡
10.教师补打卡
学生签到打卡系统
- 1.管理员端功能介绍
* 1.1登录界面
- 1.2学生打卡情况
- 1.3按时间发起打卡
- 1.4学生信息管理
- 1.5修改学生信息
- 2.学生端功能介绍
* 2.1注册界面
- 2.2在线打卡
- 3.截取部分代码介绍,可参考风格学习
* 3.1UserServlet.java
- 3.2UserService.java
- 3.3UserServiceImpl.java
- 3.4UserDao.java
- 4.数据库设计
1.管理员端功能介绍
1.1登录界面
1.2学生打卡情况
可以查看已经打卡的学生和未打卡的学生。并能够删除打卡记录。
1.3按时间发起打卡
1.4学生信息管理
实现了对学生的增删改查
1.5修改学生信息
2.学生端功能介绍
2.1注册界面
2.2在线打卡
在规定时间内进行打卡,如果超过了时间限制,则无法进行打卡,而且显示未打卡。
3.截取部分代码介绍,可参考风格学习
3.1UserServlet.java
Servlet主要是用来获取请求,并处理请求。其中主要是两种,一种写的是jsp页面的请求,会前往对应的jsp页面;另一种是获取数据,并进行处理。调用业务逻辑层,并返回执行的数据访问层的方法。将数据展示在jsp页面内。
package com.sign.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sign.entity.Record;
import com.sign.entity.User;
import com.sign.service.UserService;
import com.sign.service.impl.UserServiceImpl;
public class UserServlet extends HttpServlet {
private UserService service = new UserServiceImpl();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action=request.getParameter("action");//接受请求的参数
if(action != null && action.equals("toLogin")) {//去登录页面
toLogin(request, response);
}else if(action != null && action.equals("LoginOutAct")) {
LoginOutAct(request, response);
}else if(action != null && action.equals("welcome")) {
welcome(request, response);
}else if(action != null && action.equals("userList")) {
userList(request, response);
}else if(action != null && action.equals("sjStudent")) {
sjStudent(request, response);
}else if(action != null && action.equals("toRegister")) {
toRegister(request, response);
}else if(action != null && action.equals("Register")) {
Register(request, response);
}else if(action != null && action.equals("deleteUser")) {
deleteUser(request, response);
}
else if(action != null && action.equals("toUpdateUser")) {
toUpdateUser(request, response);
}else if(action != null && action.equals("updateUser")) {
updateUser(request, response);
}else if(action != null && action.equals("toAddUser")) {
toAddUser(request, response);
}else if(action != null && action.equals("addUser")) {
addUser(request, response);
}else if(action != null && action.equals("userLike")) {
userLike(request, response);
}
}
//按照姓名查询
private void userLike(HttpServletRequest request, HttpServletResponse response) {
try {
String userName = request.getParameter("userName");
List<User> list=service.selectUserList(userName);
request.setAttribute("list" ,list);
request.getRequestDispatcher("/WEB-INF/views/user/userList.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
//添加学生
private void addUser(HttpServletRequest request, HttpServletResponse response) {
String username = request.getParameter("username");
String realname = request.getParameter("realname");
String pwd = request.getParameter("pwd");
String type =request.getParameter("type");
User user =new User();
user.setUserName(username);
user.setRealName(realname);
user.setPassword(pwd);
user.setType(type);
try {
User user1=service.selectUserByName(username);
if(user1 == null){
service.saveUser(user);
response.sendRedirect(request.getContextPath()+"/UserServlet?action=userList");
}else{
request.setAttribute("message", "该学生已存在,不能重复注册!");
response.sendRedirect(request.getContextPath()+"/UserServlet?action=userList");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 去添加页面
* @param request
* @param response
*/
private void toAddUser(HttpServletRequest request, HttpServletResponse response) {
try {
request.getRequestDispatcher("/WEB-INF/views/user/addUser.jsp").forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修改用户信息
* @param request
* @param response
*/
private void updateUser(HttpServletRequest request, HttpServletResponse response) {
String uid = request.getParameter("uid");
String username = request.getParameter("username");
String realname = request.getParameter("realname");
String password = request.getParameter("password");
String type = request.getParameter("type");
User user =new User();
user.setId(Integer.parseInt(uid));
user.setUserName(username);
user.setRealName(realname);
user.setPassword(password);
user.setType(type);
try {
service.updateUser(user);
response.sendRedirect(request.getContextPath()+"/UserServlet?action=userList");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 去修改页面
* @param request
* @param response
*/
private void toUpdateUser(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
User user = service.selectUserById(id);
request.getSession().setAttribute("user", user);
try {
request.getRequestDispatcher("/WEB-INF/views/user/toupdateUser.jsp").forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
//删除用户
private void deleteUser(HttpServletRequest request, HttpServletResponse response) {
String id = request.getParameter("id");
try {
service.deleteUser(id);
response.sendRedirect(request.getContextPath()+"/UserServlet?action=userList");
} catch (Exception e) {
e.printStackTrace();
}
}
private void sjStudent(HttpServletRequest request, HttpServletResponse response) {
try {
List<User> newList = new ArrayList<User>();
List<User> list=service.userList();
Random r = new Random();
// 通过对象方法获取随机数
int data = r.nextInt(list.size());
System.out.println("随机数是 :"+data);
newList.add(list.get(data));
request.setAttribute("list" ,newList);
request.getRequestDispatcher("/WEB-INF/views/user/userList.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void userList(HttpServletRequest request, HttpServletResponse response) {
try {
List<User> list=service.userList();
request.setAttribute("list" ,list);
request.getRequestDispatcher("/WEB-INF/views/user/userList.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 去注册页面
* @param request
* @param response
*/
private void toRegister(HttpServletRequest request, HttpServletResponse response) {
try {
request.getRequestDispatcher("/WEB-INF/views/register.jsp").forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void welcome(HttpServletRequest request,
HttpServletResponse response) {
try {
request.getRequestDispatcher("/WEB-INF/views/console.jsp").forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}
private void LoginOutAct(HttpServletRequest request,
HttpServletResponse response) {
try {
request.getSession().removeAttribute("user");
response.sendRedirect(request.getContextPath()+"/UserServlet?action=toLogin");
} catch (Exception e) {
e.printStackTrace();
}
}
private void removeAll(HttpServletRequest request,
HttpServletResponse response) {
request.getSession().removeAttribute("user");
}
private void toLogin(HttpServletRequest request,
HttpServletResponse response) {
try {
request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}
return;
}
}
3.2UserService.java
业务逻辑层,定义了所有的方法。
package com.sign.service;
import java.util.List;
import com.sign.entity.User;
public interface UserService {
User selectUser(String userName, String password, String type);
List<User> userList();
void saveUser(User user);
User selectUserByName(String userName);
void deleteUser(String id);
}
3.3UserServiceImpl.java
继承接口,实现方法,并去调用对应的数据访问层的方法。
package com.sign.service.impl;
import java.util.List;
import com.sign.dao.UserDao;
import com.sign.entity.User;
import com.sign.service.UserService;
public class UserServiceImpl implements UserService{
private UserDao dao = new UserDao();
@Override
public List<User> selectUserList(String userName) {
return dao.selectUserList(userName);
}
@Override
public void deleteUser(String id) {
dao.deleteUser(id);;
}
@Override
public User selectUser(String userName, String password, String type) {
return dao.selectUser(userName,password,type);
}
@Override
public List<User> userList() {
return dao.userList();
}
@Override
public User selectUserByName(String userName) {
return dao.selectUserByName(userName);
}
}
3.4UserDao.java
package com.sign.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.sign.entity.Record;
import com.sign.entity.User;
import com.sign.utils.C3p0Utils;
public class UserDao {
private QueryRunner runner=new QueryRunner(C3p0Utils.getDs());
public List<User> selectUserList(String name) {
String sql="select * from user where 1 = 1";
List<User> list=null;
List<String> list1 = new ArrayList<String>();
Object[] params = {};
if (name != null && !name.equals("")) {
sql += " and realName like ? ";
list1.add("%" + name + "%");
}
if(list1.size() > 0){
params = list1.toArray();
}
sql += " order by id desc ";
try {
list=runner.query(sql, params, new BeanListHandler<User>(User.class));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return list;
}
public void updateUser(User user) {
try {
runner.update("update user set userName=?,realName=?,password=?,type=? where id =?",
user.getUserName(),user.getRealName(),user.getPassword(),user.getType(),user.getId());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void deleteUser(String id) {
Integer ids = Integer.parseInt(id);
try {
runner.update("delete from user where id=?",
ids);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void saveUser(User user) {
try {
runner.update("insert into user (username,realname,password,type) values (?,?,?,?)",
user.getUserName(),user.getRealName(),user.getPassword(),user.getType());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public User selectUser(String userName, String password, String type) {
try {//返回查询的信息
return runner.query("select * from user where userName =? and password=? and type =? ",
new BeanHandler<User>(User.class),userName,password,type);
} catch (Exception e) {
throw new RuntimeException(e);//抛出运行异常
}
}
}
4.数据库设计
-- ----------------------------
-- Table structure for dk
-- ----------------------------
DROP TABLE IF EXISTS `dk`;
CREATE TABLE `dk` (
`id` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`dkTime` varchar(111) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`tid` int(11) NULL DEFAULT NULL,
`createTime` varchar(111) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`tname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of dk
-- ----------------------------
INSERT INTO `dk` VALUES ('501012c4-75c5-4447-a67b-0118f44f2161', '2021-06-27 12:00:00', 1, '2021-06-27 10:43', '张老师');
INSERT INTO `dk` VALUES ('8ae021b5-b470-4c10-933c-0bc96b6b4334', '2021-06-27 13:00:00', 1, '2021-06-27 12:15', '张老师');
-- ----------------------------
-- Table structure for record
-- ----------------------------
DROP TABLE IF EXISTS `record`;
CREATE TABLE `record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`did` varchar(111) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`qdtime` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '签到时间',
`sjtime` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '实际签到时间',
`uid` int(11) NULL DEFAULT NULL COMMENT '用户id',
`state` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '0未签到 1已签到',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of record
-- ----------------------------
INSERT INTO `record` VALUES (7, '501012c4-75c5-4447-a67b-0118f44f2161', '2021-06-27 12:00:00', '2021-06-27 11:22', 2, '1');
INSERT INTO `record` VALUES (8, '501012c4-75c5-4447-a67b-0118f44f2161', '2021-06-27 12:00:00', NULL, 3, '0');
INSERT INTO `record` VALUES (9, '501012c4-75c5-4447-a67b-0118f44f2161', '2021-06-27 12:00:00', NULL, 4, '0');
INSERT INTO `record` VALUES (10, '8ae021b5-b470-4c10-933c-0bc96b6b4334', '2021-06-27 13:00:00', '2021-06-27 12:17', 2, '1');
INSERT INTO `record` VALUES (11, '8ae021b5-b470-4c10-933c-0bc96b6b4334', '2021-06-27 13:00:00', '2021-06-27 12:17', 3, '1');
INSERT INTO `record` VALUES (12, '8ae021b5-b470-4c10-933c-0bc96b6b4334', '2021-06-27 13:00:00', NULL, 4, '0');
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`realName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '1老师 2学生',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 't1', '张老师', '123456', '1');
INSERT INTO `user` VALUES (2, 's1', '张三', '123456', '2');
INSERT INTO `user` VALUES (3, 's2', '王五', '123456', '2');
INSERT INTO `user` VALUES (4, 's3', '麻子', '123456', '2');
SET FOREIGN_KEY_CHECKS = 1;
项目获取:
https://gitee.com/sinonfin/L-javaWebSha/tree/master