基于JSP+Servlet+MySQL实现实验管理系统

   日期:2021-02-18     浏览:102    评论:0    
核心提示:前言:进行Android项目的同时,JAVAEE课程也有相关的个人项目,做之前没有感受到VUE的“香”(看同行的同学用的很快乐,这个寒假来体验一波),所以还是用最基本的JSP以及Servlet来实现前后端的交互。项目环境:1、Eclipse IDE 4.14.02、Tomcat3、百度智能云-文字识别OCR-通用文字识别(高精度版)...

前言:进行Android项目的同时,JAVAEE课程也有相关的个人项目,做之前没有感受到VUE的“香”(看同行的同学用的很快乐,这个寒假来体验一波),所以还是用最基本的JSP以及Servlet来实现前后端的交互。
项目环境:
1、Eclipse IDE 4.14.0

2、Tomcat 9.0.39(鉴于当时用zip安装,进行配置时出现一大堆问题,建议使用exe安装)
3、MySQL 8.0配合navicat(相关的安装以及使用在我的另一篇博文里)

相关的环境准备,自己配置的时候没有及时记录下来,后来看到一篇不错的博文(内含编码格式问题的解决)分享给大家~

PS:一定要记得调试过程中完善编码格式,不然很可能不会出现自身想要的结果!

一、需求分析


用户分为三类:管理员、教师、学生

管理员
1、实现用户的增删改查
2、实现实验的增删改查
3、实现题库的增删改查

教师
1、查看教师信息
2、查看子实验信息
3、查看学生成绩信息

学生
1、查看学生信息
2、查看子实验信息
3、回答试题,提交得到成绩

二、数据库设计

采用MySQL8.0配合navicat设计(具体实现方法参考)

  • admin表(其实就是用户表)
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROp TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
  `id` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `password` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `style` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of admin
-- ----------------------------
INSERT INTO `admin` VALUES ('1', '202f', '123', '学生');
INSERT INTO `admin` VALUES ('2', 'admin', 'admin', '管理员');
INSERT INTO `admin` VALUES ('3', '2020s', '123456', '学生');
INSERT INTO `admin` VALUES ('4', '2020t', '123456', '教师');
INSERT INTO `admin` VALUES ('6', '203t', '163', '教师');
INSERT INTO `admin` VALUES ('7', '232s', '124', '学生');
INSERT INTO `admin` VALUES ('8', '202s', '123', '学生');
INSERT INTO `admin` VALUES ('9', '210s', '234', '学生');

  • experiment表(实验表)
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `experiment`;
CREATE TABLE `experiment` (
  `id` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `hour` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `device` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `rule` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `goal` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `tips` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `step` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of experiment
-- ----------------------------
INSERT INTO `experiment` VALUES ('1', '化学实验基本操作技能', '4', '电子天平,称量瓶子,表面皿,坩埚,药勺,药品', '用电子天平称量物品时,可采用直接称量法、递减称量法和固定质量称量法。', '1. 熟悉和了解天平的原理、构造、各部件的位置与作用。\r\n\r\n2. 学会差减法和固定质量法的称样方法及操作技术,准确称出实验给出试样或器具的质量。\r\n\r\n3. 学会正确记录测量数据和处理数据。', '1、电子天平应放置在一平稳、固定的工作台上,应避免阳光直射和远离发热器。\r\n\r\n2、电子天平应避免与大型机器或者带干扰的设备连接,已避免其他设备的干扰。\r\n\r\n3、电子天平应使天平远离带有磁性或能产生磁场的物体及设备。\r\n\r\n4、不得在具有爆炸性危险的区域内使用天平。\r\n\r\n5、不得长时间在高湿度或高粉尘的环境中使用天平。\r\n\r\n6、电子天平使用前应通电预热半小时以上。\r\n\r\n7、玻璃器皿使用前必须按规定认真清洗干净,洗净的器皿应是内壁能被水均匀润湿而不粘附水珠。\r\n\r\n8、吸量管和移液管等玻璃量器应先经润洗后方可移取溶液。', '(1)准备两只洁净、干燥的称量瓶,做好记号,在台秤上粗略称其质量。\r\n\r\n(2)将一块表面皿置于分析天平的托盘上,准确称取其质量。\r\n\r\n(3)用药匙将试样加到表面皿中央,开始时加入少量试样,一直到接近所需的药品量时,用右手拇指和中指及手心拿稳药勺,伸向表面皿中心部分上方,食指慢慢轻敲药勺柄,让试样慢慢落入表面皿中,直至达到要求称取质量(0.5884g)时,正确记录测量数据。\r\n\r\n(4)同步骤(2)、(3),称取第2份试样于第2个表面皿中。');
INSERT INTO `experiment` VALUES ('2', '乙酸乙酯的制备', '4', '无水乙醇、冰醋酸、浓H2SO4、饱和Na2CO3、饱和NaCl、饱和CaCl2电热套、球形冷凝管、圆底烧瓶、分液漏斗', '化学公式', '1.了解从有机酸合成酯的一般原理及方法。\r\n\r\n2. 掌握蒸馏、分液漏斗的使用等操作。', '注意操作的先后顺序', '1.在50ml圆底烧瓶中加入9.5ml(0.2mol)无水乙醇和6ml(0.1mol)冰醋酸,再小心加入2.5ml浓H2SO4,混匀后,加入沸石,装上冷凝管。2.小心加热反应瓶,缓慢回流1/2h,冷却反应物,将回流改成蒸馏装置,接受瓶用冷水冷却,蒸出生成的乙酸乙酯,直到镏出液约为反应物总体积的1/2为止。\r\n\r\n3.在镏出液中慢慢加入饱和Na2CO3,振荡,至不再有CO2气体产生为止。4.得粗产品,计算产率。');
INSERT INTO `experiment` VALUES ('3', ' \r\n原电池电动势和电极电势的测定', '4', '电位差计,铜片电极,锌片电极,导线,接线板,盐桥,移液管(5mL,4支),烧杯,玻璃棒,天平,洗耳球,洗瓶,药品', '氧化还原反应的本质是氧化剂和还原剂之间发生电子的转移。原电池中得到电子的物质是氧化剂,失去电子的物质是还原剂。物质得失电子能力的大小(或氧化还原能力的强弱)可以用该物质的氧化态-还原态所组成的电对的电极电势的相对高低来衡量。', '1.掌握原电池的组成和电池电动势的测定方法。\r\n\r\n2.了解氧化态(或还原态)浓度变化、配合物形成和沉淀生成对电极电势的影响。', '比较实验步骤1,2,3,所测得的电动势数据差异', '分别配制1.0 mol的CuSO4和 ZnSO4 溶液50ml,用细砂纸除去金属铜、锌片表面的氧化层,洗净、擦干。在30mL的烧杯中加入1.0 molCuSO4溶液15mL,并插入铜电极,组成一个半电池;在另一个30mL的烧杯中加入1.0 molZnSO4溶液15mL,并插入锌电极,组成另一个半电池。用导线将铜、锌片分别与酸度计的正负极相连,两烧杯中间以盐桥连接。用电位计测出原电池');
INSERT INTO `experiment` VALUES ('5', '\r\n样品的研磨及分级筛分', '4', '振筛机 ', '逆时针旋转手柄,提起上盖,将其固定在立杆上,取下筛具,依次将各标准筛内的物料取出', '本实验主要是对制备的LiFePO4/C材料进行研磨和筛分处理,了解样品的常用研磨方法,了解确定样品粒度组成的方法,掌握标准筛和颗粒粒径的相关知识,了解振筛机的结构和工作原理、操作方法及使用注意事项,将LiFePO4/C进行研磨和标准筛筛分,探讨样品粒度对LiFePO4/C正极材料性能的影响。', '1. 为了稳固振筛机,应用四个地脚将振筛机固定牢固,以免移动。\r\n\r\n2. 定期检查、加注、更换电动机内的机油。\r\n\r\n3. 每次筛分结束后,应擦干净振筛机上筛盘内的灰尘,严禁用水冲洗。\r\n\r\n4. 振筛机工作时,严禁将身体任意部位伸入、靠近振筛机的运动部件,以免受伤。', '1. 在摆放分样筛时,应按照筛孔大小的顺序进行叠放,孔径大的在上面,孔径小的在下面,最下面放置一个筛子底盘。\r\n\r\n2. 将需要进行筛分的试样倒入最上层的筛内,盖好筛盖,然后安放在振筛机机座内。\r\n\r\n3. 逆时针旋转夹筛盘上的提手柄,将整个夹筛盘向下滑,压紧套筛;然后,顺时针旋转夹筛盘的提手柄,其内的顶杆轴夹紧支撑套,将整套分样筛固定结实。\r\n\r\n4. 将振筛机的电源开关打开,振筛机开始工作,同时用秒表计时。\r\n\r\n5. 当振动时间达到要求的时间时,关闭电源开关,振筛机停止工作。');
INSERT INTO `experiment` VALUES ('6', '正溴丁烷的制备', '5', '正丁醇 3毫升 0.033摩尔\r\n\r\n 溴化钠(无水) 5.1克 0.05摩尔\r\n\r\n 浓硫酸 5毫升 0.094摩尔\r\n\r\n 水 4.1毫升\r\n\r\n 碳酸钠、无水氯化钙。', '本实验中正溴丁烷是由正丁醇与溴化钠、浓硫酸共热而制得', '1、 学习以溴化钠、浓硫酸和正丁醇制备正溴丁烷的原理与方法。\r\n\r\n2、 练习带有吸收有害气体装置的回流加热操作。', '1、 投料时应严格按教材上的顺序;投料后,一定要混合均匀。\r\n\r\n2、 反应时,保持回流平稳进行,防止导气管发生倒吸。', ' 1、投料;2、安装回流装置\r\n\r\n以石棉网覆盖电炉为热源,按图示安装回流装置(含气体吸收部分),注意圆底烧瓶底部与石棉网间的距离和防止碱液被倒吸。\r\n\r\n3、 加热回流\r\n\r\n在石棉网上加热至沸,调整圆底烧瓶底部与石棉网的距离,以保持沸腾而又平稳回流,并时加摇动烧瓶促使反应完成。反应约30-40min。(注意调整距离和摇动烧瓶的操作)。\r\n\r\n4、 分离粗产物\r\n\r\n待反应液冷却后,改回流装置为蒸馏装置(用直形冷凝管冷凝),蒸出粗产物。(注意判断粗产物是否蒸完)。\r\n\r\n5、收集产物');

  • student表(学生信息表)
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `age` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `sex` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `position` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `classname` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `address` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `phone` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `namestr` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `nam` (`namestr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '黄辉', '21', '男', '班长', '材料1班', '湖南省长沙市湖南大学天马1区', '18916472831', '202f');
INSERT INTO `student` VALUES ('2', '哈根', '22', '男', '纪律委员', '材料2班', '湖南省长沙市湖南大学天马2区', '13457523523', '2020s');
INSERT INTO `student` VALUES ('3', '周修', '21', '女', '副班长', '材料1班', '湖南省长沙市湖南大学天马3区', '17445546231', '232s');
INSERT INTO `student` VALUES ('4', '朱怀', '20', '男', '纪律委员', '材料1班', '湖南省长沙市湖南大学天马3区', '15342321312', '202s');
INSERT INTO `student` VALUES ('5', '曾萨', '22', '男', '权益委员', '材料1班', '湖南省长沙市湖南大学天马1区', '19834521312', '210s');

  • teacher表(教师信息表)
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `id` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `age` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `sex` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `position` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `phone` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `namestr` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `namestring` (`namestr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('1', '陈骄', '46', '男', '教授', '湖南省长沙市岳麓区天马公寓10栋', '15372892042', '2020t');
INSERT INTO `teacher` VALUES ('2', '莫伐', '53', '女', '讲师', '湖南省长沙市岳麓区望麓公寓3栋', '17634323231', '203t');

  • test表(试题表)
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `acho` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `bcho` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `ccho` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `answer` varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', '吸湿性强、遇水释放较多热量的化学品沾染皮肤后应立刻( )。', '用清水清洗', '用冷水清洗', '用软纸、软布抹去', 'C');
INSERT INTO `test` VALUES ('10', '应如何简单辨认有味的化学药品?', '用鼻子对着瓶口去辨认气味', '用舌头品尝试剂', '将瓶口远离鼻子,用手在瓶口上方扇动,稍闻其味即可', 'C');
INSERT INTO `test` VALUES ('12', '2020s', 'sdaf', 'asdf', 'rwetwer', 'B');
INSERT INTO `test` VALUES ('2', '化学品的安全标签警示词不包括( )。', '小心', '危险', '警告', 'A');
INSERT INTO `test` VALUES ('3', '对危险废物的容器和包装物以及收集、贮存、运输、处置危险废物的设施、场所,必须( )。', '设置危险废物识别标志', '设置生活垃圾识别标志', '不用设置识别标志', 'A');
INSERT INTO `test` VALUES ('4', '普通塑料、有机玻璃制品的加热温度不能超过( )。', '40℃', '60℃', '80℃', 'B');
INSERT INTO `test` VALUES ('5', '取用试剂时,错的说法是( )。', '不能用手接触试剂,以免危害健康和沾污试剂', '瓶塞应倒置桌面上,以免弄脏,取用试剂后,立即盖严,将试剂瓶放回原处,标签朝外', '多取的试剂可倒回原瓶,避免浪费', 'C');
INSERT INTO `test` VALUES ('6', '下列不可以放入105℃干燥箱干燥的是( )。', '烧杯', '量筒', '锥形瓶', 'B');
INSERT INTO `test` VALUES ('7', '实验室内使用乙炔气时,说法正确的是( )。', '室内不可有明火,不可有产生电火花的电器', '房间应密闭', '室内应有高湿度', 'A');
INSERT INTO `test` VALUES ('8', '进行腐蚀品的装卸作业应该戴( )手套。', '帆布', '橡胶', '棉布', 'B');
INSERT INTO `test` VALUES ('9', '干粉灭火器适用于( )。', '电器起火', '可燃气体起火', '以上都是', 'C');

  • score(成绩表)
-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
  `id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `experiment` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `result` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES ('122', '哈根', '乙酸乙酯的制备', '90');

三、数据库封装

  • 1、导入相关jar包

    2、连接MySQL数据库类(一般放在dao层)
package dao;

import java.sql.*;
 
public class DbHelper { 
	static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/myproject?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT";
	private static String userName = "root"; 
	private static String passWord = "huangzhihui@11"; 
	private static Connection conn = null;
 
	private DbHelper() { 
 
	}
 
	public static Connection getConnection() { 
		if (null == conn) { 
			try { 
				Class.forName(JDBC_DRIVER);
				conn = DriverManager.getConnection(url, userName, passWord);
			} catch (Exception e) { 
				e.printStackTrace();
			}
		}
		return conn;
	}
 
	public static void main(String[] args) {  
		System.err.println(getConnection());
	}
	
}

四、实现效果

登录首页

管理员用户管理界面

管理员用户添加界面

管理员用户信息修改界面

管理员用户信息删除界面

管理员实验管理界面

管理员实验添加界面

管理员实验信息修改界面

管理员题库管理界面

管理员题目增加界面

管理员题目修改界面

教师个人信息页面

教师个人信息修改页面

教师子实验信息页面

教师所属学生信息页面

教师查看学生成绩页面

学生个人信息页面

学生个人信息修改页面

学生子实验信息页面

学生子实验测试页面

学生测试结果页面


五、基础代码

  1. 整体的结构还是很简单的,每一个功能实现几乎是共通的!

NO.1 dao层的管理类

即实现我们对数据库操作的sql语句
例如:我书写的Admindao

package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
 
import entity.Admin;
 
public class AdminDao { 
	public List<Admin> getAllAdmin() {  
		List<Admin> list = new ArrayList<Admin>(); 
		Connection conn = DbHelper.getConnection();
		String sql = "select *from admin order by cast(id as decimal)"; 
		try { 
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) { 
				Admin admin = new Admin();
				admin.setId(rst.getString("id")); 
				admin.setName(rst.getString("name"));
				admin.setPassword(rst.getString("password"));
				admin.setStyle(rst.getString("style"));
				list.add(admin);
			}
			rst.close(); 
			pst.close(); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		}
		return list; 
	}
 
	public boolean addAdmin(Admin admin) {  
		String sql = "INSERT INTO `admin`(`id`,`name`,`password`,`style`) VALUES (?,?,?,?)"; 
		Connection conn = DbHelper.getConnection();
		try { 
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, admin.getId());
			pst.setString(2, admin.getName());
			pst.setString(3, admin.getPassword());
			pst.setString(4, admin.getStyle());
			int count = pst.executeUpdate();
			pst.close();
			return count > 0 ? true : false; 
		} catch (SQLException e) { 
			e.printStackTrace();
		}
		return false;
	}
 
	public boolean updateAdmin(Admin admin) {  
		String sql = "UPDATE `admin` SET `name`=?,`password`=?,`style`=? WHERe `id` = ?"; 
		Connection conn = DbHelper.getConnection();
		try { 
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, admin.getName());
			pst.setString(2, admin.getPassword());
			pst.setString(4, admin.getId()); 
			pst.setString(3, admin.getStyle());
			int count = pst.executeUpdate();
			pst.close(); 
			return count > 0 ? true : false; 
		} catch (SQLException e) { 
			e.printStackTrace();
		}
		return false;
	}
 
	public boolean deleteAdmin(int id) {  
		String sql = "delete from admin where id = ?"; 
		Connection conn = DbHelper.getConnection();
		try { 
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setInt(1, id);
			int count = pst.executeUpdate();
			pst.close();
			return count > 0 ? true : false; 
		} catch (SQLException e) { 
			e.printStackTrace();
		}
		return false;
	}
 
	public Admin selectAdminById(int id) {  
		Connection conn = DbHelper.getConnection();
		String sql = "select *from admin where id = " + id;
		Admin admin = null;
		try { 
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) { 
				admin = new Admin();
				admin.setId(rst.getString("id"));
				admin.setName(rst.getString("name"));
				admin.setPassword(rst.getString("password"));
				admin.setStyle(rst.getString("style"));
			}
			rst.close();
			pst.close();
		} catch (SQLException e) { 
			e.printStackTrace();
		}
		return admin; 
	}
}

NO.2 entity层的实体类

即实现对数据库字段的set和get方法
例如:我书写的Admin

package entity;

import java.io.Serializable;
 
public class Admin implements Serializable {  
 
	private static final long serialVersionUID = 1L;
 
	private String id;
	private String name;
	private String password;
	private String style;
	
	public String getId() { 
		return id;
	}
	public void setId(String id) { 
		this.id = id;
	}
	public String getName() { 
		return name;
	}
	public void setName(String name) { 
		this.name = name;
	}
	public String getPassword() { 
		return password;
	}
	public void setPassword(String password) { 
		this.password = password;
	}
	public static long getSerialversionuid() { 
		return serialVersionUID;
	}
	public String getStyle() { 
		return style;
	}
	public void setStyle(String style) { 
		this.style = style;
	}
 
}

NO.3 servlet层的实现类

即实现对数据库记录的增删改查方法
例如:我书写的AddServlet、DeleteServlet、UpdateServlet、ShowServlet

增  AddServlet.java
package servlet;
import java.io.IOException; 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import dao.AdminDao;
import entity.Admin;

public class AddServlet extends HttpServlet { 
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
		this.doPost(req, resp);
	}
 
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
		req.setCharacterEncoding("UTF-8");
		
		String id=req.getParameter("id");
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		String style=req.getParameter("style");
		Admin admin = new Admin();
		admin.setId(id);
		admin.setName(name); 
		admin.setPassword(password);
		admin.setStyle(style);
		AdminDao dao = new AdminDao();
		dao.addAdmin(admin);
		req.getRequestDispatcher("ShowServlet").forward(req, resp);
	}
}
删 DeleteServlet.java
package servlet;

import java.io.IOException; 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import dao.AdminDao;
 
public class DeleteServlet extends HttpServlet {  
 
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
		this.doPost(req, resp);
	}
 
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
		String idStr = req.getParameter("id"); 
		if (idStr != null && !idStr.equals("")) { 
			int id = Integer.valueOf(idStr);
			AdminDao dao = new AdminDao();
			dao.deleteAdmin(id);
		}
		req.getRequestDispatcher("ShowServlet").forward(req, resp);
	}
 
}
改 UpdateServlet.java
package servlet;

import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import dao.AdminDao;
import entity.Admin;
 
public class UpdateServlet extends HttpServlet{   
 
    private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
		String idStr = req.getParameter("id");
		if (idStr != null && !idStr.equals("")) { 
			int id = Integer.valueOf(idStr);
			AdminDao dao = new AdminDao();
			Admin admin = dao.selectAdminById(id);
			req.setAttribute("admin", admin);
		}
		req.getRequestDispatcher("admin/admin_user_update.jsp").forward(req, resp);
 
	}
 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {   
    	
    	req.setCharacterEncoding("UTF-8");
    	resp.setCharacterEncoding("UTF-8");
        String username = req.getParameter("name");
        String userpwd = req.getParameter("password");
        String id = req.getParameter("id");
        String style=req.getParameter("style");
        Admin admin = new Admin();
        admin.setId(id);
        admin.setName(username);
        admin.setPassword(userpwd);
        admin.setStyle(style);
        AdminDao dao = new AdminDao();
        dao.updateAdmin(admin);
        req.getRequestDispatcher("ShowServlet").forward(req, resp);
    }
    
    
 
}
查 ShowServlet.java
package servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AdminDao;
import entity.Admin;
 
public class ShowServlet extends HttpServlet {  
 
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		this.doPost(req, resp);
	}
 
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
		
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		AdminDao dao = new AdminDao();
		List<Admin> list = dao.getAllAdmin();
		req.setAttribute("list", list);
		req.getRequestDispatcher("admin/admin_user_index.jsp").forward(req, resp);
	}
}

NO.4 jsp层的页面类

即实现搭配servlet的显示页面
例如:我书写的admin_user_index、admin_user_add、admin_user_update

admin_user_index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@page import="entity.Admin"%>
    <%@page import="dao.*" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" >
<title>显示</title>
    <style type="text/css">
           
			form h1 { 
				display: inline-block;
				width: 820px;
			}
			.check { 
				padding: 30px 30px;
			}
			
			form { 
				display: inline;
			}
			
			.contain { 
				width: 1298px;
				border-radius: 10px;
				border: 1px #bbb solid;
				background-color: #eee;
				padding-top: 20px;
			}
			
			button { 
				margin-left: 10px;
			}
			
			
			.add { 
				float: right;
				margin-right: 50px;
			}
			
    </style>
</head>
<body>
<div class="contain pull-left">
    <h2>用户账号管理</h2>
    
    <a href="../admin/admin_user_add.jsp" target="_self"><button class="btn btn-primary add">添加用户</button></a>
  
    <div class="pull-left">
    <table class="table table-hover">
    <thead>
        <tr>
            <td align="center">编号</td>
            <td align="center">帐号</td>
            <td align="center">密码</td>
            <td align="center">类别</td>
            <td align="center">操作</td>
        </tr>
        
<% 
    java.util.List<Admin> adminList = new java.util.ArrayList<Admin>();
    AdminDao admindao=new AdminDao();
	adminList = admindao.getAllAdmin();	
	for(Admin admin : adminList){ 
%>
    <tr>
    <td align="center"><%=admin.getId()%></td>
    <td align="center"><%=admin.getName()%></td>
    <td align="center"><%=admin.getPassword() %></td>
    <td align="center"><%=admin.getStyle() %></td>
    
    <td align="center">
    <a href="..//UpdateServlet?id=<%=admin.getId() %>"
    onclick='return window.confirm("This user will be changed. Are you sure?")'><button class='btn btn-primary'>修改</button></a>
    |<a href="..//DeleteServlet?id=<%=admin.getId() %>" 
    onclick='return window.confirm("This user will be deleted. Are you sure?")'><button class='btn btn-danger'>删除</button></a>
    </td>
    <%} %>
  </tr> 
        
        </thead>
    </table>
    </div>
    </div>
</body>
</html>
admin_user_add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" >
   <title>添加</title>
    <style type="text/css">
           
			form h1 { 
				display: inline-block;
				width: 820px;
			}
			.check { 
				padding: 30px 30px;
			}
			
			form { 
				display: inline;
			}
			
			.contain { 
				width: 1298px;
				border-radius: 10px;
				border: 1px #bbb solid;
				background-color: #eee;
				padding-top: 20px;
			}
			
			button { 
				margin-left: 10px;
			}
			
			
			.add { 
				float: right;
				margin-right: 50px;
			}
			
    </style>

  </head>
 
  <body>
  <div class="contain pull-left">
  <h2>用户账号添加</h2>
  
  <div class="pull-left">
  
  <form action="../AddServlet" method="post">
    <table class="table table-hover">
    <thead>
        <tr>
            <td>用户id:</td>
            <td><input type="text" style="width: 40%" name="id" required/></td>
        </tr>
        <tr>
            <td>用户帐号:</td>
            <td><input  type="text" style="width: 40%" name="name" required/></td>
        </tr>
        <tr>
            <td>用户密码:</td>
            <td><input  type="password" style="width: 40%" name="password" required/></td>
        </tr>
        <tr>
            <td>用户类别:</td>
            <td>
            <select name="style" style="width: 40%">
            <option>管理员</option>
            <option>学生</option>
            <option>教师</option>
            </select>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input  type="submit" class='btn btn-primary' value="提交"/>
                <input  type="reset"  class='btn btn-warning' value="清空"/>
                <input  type="button"  class='btn btn-success'  value="返回" onclick="history.go(-1)"/>
            </td>
        </tr>
        </thead>
    </table>
   </form>
   </div>
   </div>
  </body>
</html>
admin_user_update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" >
    <title>修改</title>
    <style type="text/css">
           
			form h1 { 
				display: inline-block;
				width: 820px;
			}
			.check { 
				padding: 30px 30px;
			}
			
			form { 
				display: inline;
			}
			
			.contain { 
				width: 1298px;
				border-radius: 10px;
				border: 1px #bbb solid;
				background-color: #eee;
				padding-top: 20px;
			}
			
			button { 
				margin-left: 10px;
			}
			
			
			.add { 
				float: right;
				margin-right: 50px;
			}
			
    </style>
  </head>
  
  <body>
  <div class="contain pull-left">
  <h2>账号信息修改</h2>
  
  <div class="pull-left">
    
    <form action="UpdateServlet" method="post">
    <table class="table table-hover">
    <thead>
        <tr>
            <td>用户编号:</td>
            <td><input  type="text"  style="width: 40%" name="id" value="${admin.id}" required/></td>
        </tr>
        
        <tr>
            <td>用户帐号:</td>
            <td><input  type="text"  style="width: 40%" name="name" value="${admin.name}" required/></td>
        </tr>
        <tr>
            <td>用户密码:</td>
            <td><input  type="text"  style="width: 40%" name="password" value="${admin.password}" required/></td>
        </tr>
        <tr>
            <td>用户类别:</td>
            <td><input  type="text"  style="width: 40%" name="style" value="${admin.style}"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <input  type="submit"  class='btn btn-primary'  value="提交"/>
                <input  type="button"  class='btn btn-success'  value="返回" onclick="history.go(-1)"/>
            </td>
        </tr>
    </thead>
    </table>
   </form>
   </div>
   </div>
  </body>
</html>

六、框架集显示

  1. 由于当时没有用VUE的框架,所以用的HTML框架来实现点击后的右侧刷新效果!
frame.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理员页面</title>
</head>
<frameset rows="11%,*"  framespacing="10">
		<frame src="admin/Top.jsp" noresize="noresize" scrolling="yes" />
		<frameset cols="15%,*">
			<frame src="admin/Left_nav.jsp" noresize="noresize" />
			<frame src="admin/admin_user_index.jsp" name="contain" />
		</frameset>
	</frameset>
</html>
Top.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理员</title>
<style type="text/css">
	.block{ 
		width: 1300px;
		height: 50px;
		background-color: #ccc;
		border-radius: 10px;
		margin: 0 auto;
	}
	h3{ 
		text-align: center;
		padding-top: 12px;
		font-family: "微软雅黑";
	}
</style>
</head>
<body>
	<div class="block">
		<h3>您好!管理员,欢迎使用材料学院实验管理系统!</h3>
	</div>
</body>
</html>
Left_nav.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
		<style type="text/css">
			ul{ 
				width: 200px;
				height:400px;
				float: right;
				margin-top: 30px;
				font-family: "微软雅黑";
			}
		</style>
	</head>
	<body>
		<ul class="nav nav-pills nav-stacked">
			<li style="display: inline-block;background-color:black;font-size: 2em"><a href="admin_user_index.jsp" target="contain">用户管理</a></li>
			<li style="display: inline-block;background-color:black;font-size: 2em"><a href="admin_experiment_index.jsp" target="contain">实验管理</a></li>
			<li style="display: inline-block;background-color:black;font-size: 2em"><a href="admin_test_index.jsp" target="contain">题库管理</a></li>
			<li style="display: inline-block;background-color:black;font-size: 2em"><a href="../index.jsp" target="_parent">退出系统</a></li>
			
		</ul>

	</body>
</html>

第一次进行WEB项目,写的略显潦草,后期有待优化!需要源码的朋友留下邮箱,我会直接打包发给你们。之后有时间我会上传到GitHub上!期待大家的指教!

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服