基于Java的英语单词测试系统

本文最后更新于:2023年6月13日 晚上

1.Preface前言

This a page for the Course Design Project of OOPJava

2.Git地址

https://github.com/Godtokoo666/projectx
由于本项目是使用Maven构建和管理的,所以Git中并不提供jar包,因此您看不到属于jar包的lib文件夹。但除jdk11所提供的所有jar包外,所需的所有文件都可以借由Maven的配置文件pom.xml看到,包括但不限于MySQL Connector/J、JavaServer Pages(TM) API

3.项目概览

整体介绍

本着面向对象程序设计“高内聚,低耦合”的思想,本项目借鉴了MVC模式中的内容,即三个基本部分:模型(Model)视图(View)控制器(Controller)。在MVC中,存在三层架构,即表示层(USL,User Show Layer)业务逻辑层(BLL,Business Logic Layer)数据访问层(DAL,Data Access Layer)。本项目基本融合了三层架构和MVC模式的思想。作为一个单词测试系统,本项目实现了基本的登录和注册功能,实现了基本的单词测试功能,实现了用户数据的查询以及单词的添加功能。

结构解释

在IntelliJ IDEA中,截得的项目源代码结构图如下
图1-项目源代码结构图
如您所见,src文件夹中存在着Bean、Dao、Servlet三个包以及一个webapp文件夹,其中Bean包内的类作为实体类将各种数据进行封装,以便使数据在三层架构间进行流通。
图2-三层架构图
webapp中的内容作为表示层部分,用于处理用户请求。在本项目中,webapp主要处理注册登录、单词测试数据的表单提交、个人数据的查询以及管理员页面的全局数据查询和单词的添加操作。Servlet包中的类作为业务逻辑层,承上启下,继承HttpServlet用于接收request,并通过具体代码与Dao包中的内容进行交互或通过session把请求直接实现。Dao包中的类作为数据访问层,由于该层直接通过JDBC与数据库交互,因此也起着最底层的数据访问作用。
对于数据的储存,本项目使用MySQL数据库,其表结构如下:
图3-MySQL表结构
其中,出于轻量化的目的,本项目user表同时存储用户名和明文密码以及单词测试产生的数据。但我们并不建议您这么错,更为安全的做法是将password转码加密或者使用其他方式储存。word表存储单词的英文和中文属性。此外,为了拓展功能,后续可添加词性或其他属性对单词进行分类。test表用于存储测试数据,但在此项目的第一版中,我们并未投入使用,后续将考虑进行延申。

4.功能实现

功能架构图:
图13-功能架构图

1.前端主页面

图4-前端主页面
源代码:index.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
71
72
73
74
75
76
77
78
79
80
<%@ page import="Dao.WordsDao" %>
<%@ page import="Bean.Word" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<title>
单词测试系统
</title>
<link href="css/main.css" rel="stylesheet" media="screen">
</head>
<body>
<div style="display: flex;margin: 5vh;justify-content: center;height: 20vh;font-size: 30px;font-family: FangSong">
<h1 >基于Java的单词测试系统<br><p style="text-align: center;font-size: 30px">Word Testing System Based on Java </p></h1>
</div>
<div class="container-mi">
<div class="box">
<h2 style="text-align: center">
单词测试:根据您所在的组进行测试
</h2>
<div style="text-align: center">
<a href="wordstest.jsp" target="_blank">
<button type="button" class="btn btn-large btn-primary" data-toggle="button">点击进入单词测试</button>
</a>
</div>
</div>
<div class="box">
<h2 style="text-align: center">
单词列表:输出单词列表
</h2>
<div style="text-align: center">
<a href="list.jsp" target="_blank">
<button type="button" class="btn btn-large btn-primary" data-toggle="button">点击进入单词列表</button>
</a>
</div>
</div>
<div class="box">
<h2 style="text-align: center">
个人中心:您的测试及练习数据中心
</h2>
<div style="text-align: center">
<a href="usercenter.jsp" target="_blank">
<button type="button" class="btn btn-large btn-primary" data-toggle="button">点击进入个人中心</button>
</a>
</div>
</div>
</div>
<% String username = (String) request.getSession().getAttribute("user");
if (username == null) {
%>
<div style="text-align: center">
您好,请登录
<br>
<a href="login.jsp">login</a>
</div>
<% }
else {
%>
<div style="text-align: center">
您好,<%=(String) request.getSession().getAttribute("user")%>
<br>
<a href="logout">logout</a>
</div>
<% }%>
</form>
</body>
<body>
<%--预加载List资源--%>
<%
WordsDao wordsDao = new WordsDao();
List<Word> list =new ArrayList<>();
try {
list = wordsDao.getList();
} catch (Exception e) {
}
request.getSession().setAttribute("list",list);
%>
</body>
</html>

2.登录与注册

前端页面:

图5-登录界面
图6-注册界面
源代码:login.jsp、register.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
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #f5f5f5;
}

.box-lo {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 300px;
height: 300px;
border-radius: 50px;
background: #e0e0e0;
box-shadow: 20px 20px 60px #bebebe, -20px -20px 60px #ffffff;
}

.centered-form {
text-align: center;
}

.form-title {
font-size: 18px;
margin-bottom: 10px;
}
</style>
<body>
<div class="box-lo">
<div class="centered-form">
<div class="form-title">用户登录</div>
<form name="frmlogin" method="post" action="doLogin">
用户名:<input name="username" type="text"/><br/>
密码:<input name="password" type="password"/><br/>
<input name="resetbtn" type="reset" value="重置"/>
<input name="loginbtn" type="submit" value="登录"/>
<button onclick="window.location.href='register.jsp'" type="button">注册</button>
</form>
</div>
</div>
</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
47
48
49
50
51
52
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户注册</title>
</head>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #f5f5f5;
}

.box-lo {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 300px;
height: 300px;
border-radius: 50px;
background: #e0e0e0;
box-shadow: 20px 20px 60px #bebebe, -20px -20px 60px #ffffff;
}

.centered-form {
text-align: center;
}

.form-title {
font-size: 18px;
margin-bottom: 10px;
}
</style>
<body>
<div class="box-lo">
<div class="centered-form">
<div class="form-title">用户注册</div>
<form name="frmregister" method="post" action="doRegister">
用户名:<input name="username" type="text"/><br/>
密码:<input name="password" type="password"/><br/>
确认密码:<input name="confirmpassword" type="password"/><br/>
<input name="resetbtn" type="reset" value="重置"/>
<input name="loginbtn" type="submit" value="注册"/>
<button onclick="window.location.href='login.jsp'" type="button">登录</button>
</form>
</div>
</div>
</body>
</html>

具体实现:

1.对于登录操作,在前端jsp页面中,我们使用表单form将username以及password提交到doLoginServlet,Servlet将request处理,通过UserDao与数据库交互,判断账号密码是否正确,此后设置session“user”的值,并用于前端展示和用户身份过滤。
2.对于注册操作,同样获取表单数据后,提交到doRegisterServlet,Servlet将request处理,通过UserDao与数据库交互,将注册数据导入数据库,然后将页面重定向到登录页面。

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
//doLoginServlet.java

package Servlet;
import Bean.*;
import Dao.*;
import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/doLogin")
public class doLoginServlet extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
ServletContext application = req.getServletContext();
application.setAttribute("username", username);

//利用session储存异常信息
HttpSession session = req.getSession();
//利用session检查是否登录
HttpSession check = req.getSession();
//去除空格导致的错误
if (username == null || "".equals(username.trim())) {
session.setAttribute("error", "用户名输入错误");
//能使用请求转发和重定向,优先使用重定向,防止恶意占用资源
res.sendRedirect(req.getContextPath() + "/login.jsp");
return;
}
if (password == null || "".equals(password.trim())) {
session.setAttribute("error", "密码输入错误");
//能使用请求转发和重定向,优先使用重定向,防止恶意占用资源
res.sendRedirect(req.getContextPath() + "/login.jsp");
return;
}
UserDao userDao = new UserDao();
User user = null;
try {
user = userDao.UserLogin(username,password);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}

if (user == null || user.equals("")) {
session.setAttribute("error", "用户名或密码错误");
res.sendRedirect(req.getContextPath() + "/login.jsp");
} else if (user.getCategory().equals("admin")) {
session.setAttribute("user", "admin");
req.getRequestDispatcher("/index.jsp").forward(req, res);

} else {
session.setAttribute("user", username);
req.getRequestDispatcher("/index.jsp").forward(req, res);
}
}
}


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
//doRegisterServlet.java

package Servlet;
import Bean.*;
import Dao.UserDao;

import java.io.*;
import java.sql.SQLException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.*;
import javax.servlet.*;
@WebServlet("/doRegister")

public class doRegisterServlet extends HttpServlet{
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String password1 = req.getParameter("confirmpassword");
String message = null;
if (password1.equals(password)) {
User us = new User(username, password,"user");
UserDao userDao = new UserDao();
try {
userDao.UserRegister(us);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}

resp.sendRedirect(req.getContextPath() + "/login.jsp");

} else {
message = "密码不一致!";
req.setAttribute("Message", message);
req.getRequestDispatcher("/Message.jsp").forward(req, resp);
}
}
}
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
//UserDao.java

package Dao;
import Bean.*;
import java.sql.*;
public class UserDao{

private Connection connection;
public void UserRegister(User us) throws ClassNotFoundException, SQLException {
Connection connection=DBUtil.con();
String sql="insert into user(username,password,category) values(?,?,?)";
PreparedStatement preparedStatement=connection.prepareStatement(sql);{
preparedStatement.setString(1,us.getUsername());
preparedStatement.setString(2,us.getPassword());
preparedStatement.setString(3,us.getCategory());
preparedStatement.executeUpdate();
}
preparedStatement.close();
connection.close();
}
public User UserLogin(String username,String password) throws ClassNotFoundException, SQLException {
Connection connection = DBUtil.con();
Statement statement = connection.createStatement();
String sql="SELECT * FROM user WHERE username = '"+username+"'" ;
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()) {
String realPassWord = (String) resultSet.getObject("password");
String realCategory = (String) resultSet.getObject("category");
resultSet.close();
connection.close();
if (realPassWord.equals(password)) {
return new User(username,realPassWord,realCategory);
} else {
return null;
}
}
return null;
}
public void DoTest(String username,int number,int pass_number) throws ClassNotFoundException, SQLException {
Connection connection=DBUtil.con();
String sql1="SELECT * FROM user WHERE username = '"+username+"'" ;
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql1);
int num=0,pass_num=0;
if(resultSet.next())
{
num=(int)resultSet.getObject("number")+number;
pass_num=(int)resultSet.getObject("pass_number")+pass_number;
}
double main_passrate = pass_num/(double)num;
double last_passrate = pass_number/(double)number;
String sql="update user set main_passrate = ?, last_passrate = ? ,number = ? ,pass_number = ? where username ='"+username+"'";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
{
preparedStatement.setDouble(1,main_passrate);
preparedStatement.setDouble(2,last_passrate);
preparedStatement.setInt(3,num);
preparedStatement.setInt(4,pass_num);
preparedStatement.executeUpdate();
}
}
}

3.单词测试

前端页面:

图7-单词测试界面
图8-测试完成界面
源代码:wordstest.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
<%@ page import="Bean.Word" %>
<%@ page import="java.util.List" %>
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<meta charset="UTF-8">
<title>
单词测试
</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<%-- 检查用户会话 --%>
<%
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
%>
<script type="text/javascript">
alert("您需要先登录才能访问此页面");
window.location.href = "login.jsp";
</script>
<%
} else {}
%>
</body>
<body>
<h1 style="text-align: center">单词测试页面WordTest <a href="index.jsp" class="btn btn-primary"> 返回主页</a></h1>
<form name="Word" method="post" action="WordServlet">
<table class="table">
<tr>
<td>英文</td>
<td>中文</td>
</tr>
<%List<Word> list = (List<Word>)request.getSession().getAttribute("list");%>
<%
for(Word word : list)
{
out.write("<tr>");
out.write("<td>"+word.getEnglish()+"</td>");
out.write("<td><input name='"+word.getEnglish()+"' type='text'/><br/></td>");
out.write("</tr>");
}
%>

</table>
<div style="text-align: center">
<input name="submitbtn" type="submit" value="提交" ></br>
</div>
</form>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
<body>
<div style="text-align: center">
您好,<%=(String) request.getSession().getAttribute("user")%>
<br>
<a href="logout">logout</a>
</div>
</body>
</html>

具体实现

在前端jsp页面中,出于轻量化的目的,我们直接嵌入了Java代码,来将后端获取的List对象直接打印在前端页面,并为各个单词附上提交框,最后一并以表单方式提交到WordServlet。servlet为前端获取到的request数据转移至List对象中,然后调用WordsDao类中的方法,将前端获取到的List对象与数据库中获取到的List进行比较,输出错误单词、正确单词和正确率。同时,servlet调用UserDao类中的doTest方法,对数据库中的用户测试数据进行更新。

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
//WordServlet.java
package Servlet;

import Bean.Result;
import Bean.Word;
import Dao.UserDao;
import Dao.WordsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.*;

@WebServlet("/WordServlet")
public class WordServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
req.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
List<Word> list=new ArrayList<>(),answer=new ArrayList<>();
UserDao userDao =new UserDao();
WordsDao wordsDao = new WordsDao();
try {
list = wordsDao.getList();
} catch (Exception e) {
}
for(int i=0;i<list.size();i++)
{
Word w=new Word(list.get(i).getEnglish(), req.getParameter(list.get(i).getEnglish()));
answer.add(w);
}
Result result=null;
try {
result = wordsDao.judge(answer);
} catch (Exception e) {
}
try {
userDao.DoTest((String) req.getSession().getAttribute("user"),(result.right.size()+result.wrong.size()),result.right.size());
} catch (Exception e) {
}
double passrate = result.right.size()/(double)(result.right.size()+result.wrong.size());
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<h1 style='text-align: center'>提交完成,正确率为"+passrate+"</h1>");
out.println("<div style = 'text-align:center'>");
out.println("<h3>正确单词有</h3>");
for(int i=0;i<result.right.size();i++)
{
out.println("<p>"+result.right.get(i)+"</p><br/>");
}
out.println("</div>");
out.println("<div style = 'text-align:center'>");
out.println("<h3>错误单词有</h3>");
for(int i=0;i<result.wrong.size();i++)
{
out.println("<p>"+result.wrong.get(i)+"</p><br/>");
}
out.println("</div>");
out.println("</html>");
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
super.doGet(req, res);
}
}

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
//WordsDao.java
package Dao;

import Bean.Result;
import Bean.Word;

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

public class WordsDao {
private Connection connection;
public void wordInsert(String English,String Chinese) throws SQLException, ClassNotFoundException {
Connection connection = DBUtil.con();
String sql="insert into words(English,Chinese) values(?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
{
preparedStatement.setString(1,English);
preparedStatement.setString(2,Chinese);
preparedStatement.executeUpdate();
}
preparedStatement.close();
connection.close();
}
public String getChinese(String English) throws SQLException, ClassNotFoundException {
Connection connection = DBUtil.con();
String sql = "select * from words where English = '"+English+"'";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
connection.close();
if(resultSet.next())
{
return (String) resultSet.getObject("Chinese");
}
return null ;
}
public List<Word> getList() throws SQLException, ClassNotFoundException {
Connection connection=DBUtil.con();
List<Word> list = new ArrayList<>();
String sql = "select * from words";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next())
{
list.add(new Word((String)resultSet.getString("English"),(String) resultSet.getObject("Chinese")));
}
return list;
}
public Result judge(List<Word> words) throws ClassNotFoundException, SQLException {
List<String> right = new ArrayList<>();
List<String> wrong = new ArrayList<>();
Connection connection = DBUtil.con();
List<Word> list = new ArrayList<>();
String sql = "select * from words";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next())
{
list.add(new Word((String)resultSet.getString("English"),(String) resultSet.getObject("Chinese")));
}
for(int i=0;i<words.size();i++)
{
if(words.get(i).getChinese().equals(list.get(i).getChinese()))
{
right.add(words.get(i).getEnglish());
}
else
{
wrong.add(words.get(i).getEnglish());
}
}
return new Result(right,wrong);
}
}


4.数据查询

前端页面:

图9-用户个人中心页面
图10-管理后台页面
源代码:usercenter.jsp、admin.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
<%@ page import="Bean.User" %>
<%@ page import="Dao.UserDataDao" %>
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<meta charset="UTF-8">
<title>
个人中心
</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<%-- 检查用户会话 --%>
<%
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
%>
<script type="text/javascript">
alert("您需要先登录才能访问此页面");
window.location.href = "login.jsp";
</script>
<%
} else if(username == "admin")
{
%>
<script type="text/javascript">
window.location.href = "admin.jsp";
</script>
<%
} else {}
%>
</body>
<body>
<h1 style="text-align: center">用户中心 <a href="index.jsp" class="btn btn-primary"> 返回主页</a></h1>
<table class="table">
<tr>
<td>用户名</td>
<td>主通过率</td>
<td>上次测试通过率</td>
<td>总测试数</td>
<td>通过数</td>
</tr>
<%
UserDataDao userDataDao = new UserDataDao();
User user = userDataDao.rate((String) request.getSession().getAttribute("user"));
out.write("<tr>");
out.write("<td>"+user.getUsername()+"</td>");
out.write("<td>"+user.getPassRate()+"</td>");
out.write("<td>"+user.getLast_passrate()+"</td>");
out.write("<td>"+user.getNumber()+"</td>");
out.write("<td>"+user.getPassNumber()+"</td>");
out.write("</tr>");
%>
</table>
<script src="js/jquery-1.11.0.js"></script>
 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
<body>
<div style="text-align: center">
您好,<%=(String) request.getSession().getAttribute("user")%>
<br>
<a href="logout">logout</a>
</div>
</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
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
<%@ page import="java.util.ArrayList" %>
<%@ page import="Dao.AdminDataDao" %>
<%@ page import="Bean.User" %>
<%@ page import="java.util.List" %>
<%@ page import="Bean.Word" %>
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<meta charset="UTF-8">
<title>
管理后台
</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<%-- 检查用户会话 --%>
<%
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
%>
<script type="text/javascript">
alert("您需要先登录才能访问此页面");
window.location.href = "login.jsp";
</script>
<%
} else if(username != "admin")
{
%>
<script type="text/javascript">
alert("非管理员用户禁止访问");
window.location.href = "usercenter.jsp";
</script>
<%
}
else{}
%>
</body>
<body>
<h1 style="text-align: center">管理后台</h1>
<h3 style="text-align: center">用户数据 <a href="index.jsp" class="btn btn-primary"> 返回主页</a></h3>
<table class="table">
<tr>
<td>用户名</td>
<td>主通过率</td>
<td>上次测试通过率</td>
<td>总测试数</td>
<td>通过数</td>
</tr>
<%
AdminDataDao adminDataDao = new AdminDataDao();
List<User> users= adminDataDao.rate();
for(User user : users)
{
out.write("<tr>");
out.write("<td>"+user.getUsername()+"</td>");
out.write("<td>"+user.getPassRate()+"</td>");
out.write("<td>"+user.getLast_passrate()+"</td>");
out.write("<td>"+user.getNumber()+"</td>");
out.write("<td>"+user.getPassNumber()+"</td>");
out.write("</tr>");
}
%>
</table>
<h3 style="text-align: center">单词列表WordList <a href="add.jsp" target="_blank" class="btn btn-primary"> 添加单词</a></h3>
<table class="table">
<tr>
<td>英文</td>
<td>中文</td>
</tr>
<%
List<Word> list = (List<Word>)request.getSession().getAttribute("list");
for (Word word : list) {
out.write("<tr>");
out.write("<td>"+word.getEnglish()+"</td>");
out.write("<td>"+word.getChinese()+"</td>");
out.write("</tr>");

}
%>
</table>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
<body>
<div style="text-align: center">
您好,<%=(String) request.getSession().getAttribute("user")%>
<br>
<a href="logout">logout</a>
</div>
</body>
</html>

具体实现:

·对于用户,在前端jsp文件中,我们直接嵌入了Java代码,直接调用UserDataDao类中的 rate方法,输出对应用户名的各项数据,包括测试通过率,通过数等。
·对于管理员,在前端jsp文件中,我们同样直接嵌入了Java,直接调用AdminDataDao类中的rate方法,输出所有用户的各项数据。同单词测试页面,管理后台同样输出了单词的列表,但此时包含英文单词的中文释义。此外,管理后台提供了一个添加单词的按钮,用于跳转到单词添加页面。

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
//UserDataDao.java

package Dao;

import Bean.User;

import java.sql.*;

public class UserDataDao {
private Connection connection;
public User rate(String username) throws ClassNotFoundException, SQLException {
User users = new User();
Connection connection = DBUtil.con();
String sql = "select * from user where username = '" + username + "'";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
double pass_rate = (double) resultSet.getObject("main_passrate");
double last_passrate = (double) resultSet.getObject("last_passrate");
int number=(int)resultSet.getObject("number");
int pass_number=(int)resultSet.getObject("pass_number");
users = new User(username, pass_rate, last_passrate,number,pass_number);
}
return users;
}
}

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
//AdminDataDao.java
package Dao;

import Bean.User;

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

public class AdminDataDao {
private Connection connection;
public List<User> rate() throws ClassNotFoundException, SQLException {
List<User> users = new ArrayList<>();
Connection connection=DBUtil.con();
String sql = "select * from user ";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next())
{
String username = (String) resultSet.getObject("username");
double pass_rate = (double) resultSet.getObject("main_passrate");
double last_passrate= (double) resultSet.getObject("last_passrate");
int number=(int)resultSet.getObject("number");
int pass_number=(int)resultSet.getObject("pass_number");
users.add(new User(username,pass_rate,last_passrate,number,pass_number));
}
return users;
}
}

5.单词添加

前端页面:

图11-单词添加界面
源代码:add.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
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>单词添加页面</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<%-- 检查用户会话 --%>
<%
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
%>
<script type="text/javascript">
alert("您需要先登录才能访问此页面");
window.location.href = "login.jsp";
</script>
<%
} else if(username != "admin")
{
%>
<script type="text/javascript">
alert("非管理员用户禁止访问");
window.location.href = "index.jsp";
</script>
<%
}
else{}
%>
</body>
<body>
<h2>单词添加页面</h2>
<form action="add" method="post" class="form-inline">
<div class="form-group">
<label for="english">英文</label>
<input type="text" class="form-control" id="english" name="english">
</div>
<div class="form-group">
<label for="chinese">中文</label>
<input type="text" class="form-control" id="chinese" name="chinese">
</div>
<button type="submit" class="btn btn-default">提交</button>

</form>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

具体实现:

与注册类似,jsp页面发送表单数据给WordAddServlet,servlet调用WordDao类中的WordInsert方法,将数据提交到数据库,完成insert into操作。

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
//WordAddServlet.java

package Servlet;

import Dao.WordsDao;

import java.io.*;
import java.sql.SQLException;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/add")
public class WordAddServlet extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException {
req.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8") ;
String English = req.getParameter("english");
String Chinese = req.getParameter("chinese");
WordsDao wordsDao = new WordsDao();
try {
wordsDao.wordInsert(English,Chinese);
} catch (Exception e) {
System.out.println(e);
}
PrintWriter out=res.getWriter();
out.println("<html><h1>添加成功</h1><br><a href='add.jsp'>点击返回</a></html>");
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
super.doGet(req, res);
}

}

1
2
3
4
5
6
7
8
9
10
11
12
public void wordInsert(String English,String Chinese) throws SQLException, ClassNotFoundException {
Connection connection = DBUtil.con();
String sql="insert into words(English,Chinese) values(?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
{
preparedStatement.setString(1,English);
preparedStatement.setString(2,Chinese);
preparedStatement.executeUpdate();
}
preparedStatement.close();
connection.close();
}

6.单词列表List

同时,本项目实现了一个单词列表List页面,具体实现与管理后台的列表相同。
图12-单词列表List
源代码:list.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
<%@ page import="Bean.Word" %>
<%@ page import="java.util.List" %><%--
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<head>
<title>单词列表</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<%-- 检查用户会话 --%>
<%
String username = (String) request.getSession().getAttribute("user");
if (username == null) {
%>
<script type="text/javascript">
alert("您需要先登录才能访问此页面");
window.location.href = "login.jsp";
</script>
<%
} else {}
%>
</body>
<body>
<h1 style="text-align: ">单词列表WordList <a href="index.jsp" class="btn btn-primary"> 返回主页</a></h1>

<table class="table">
<tr>
<td>英文</td>
<td>中文</td>
</tr>
<%
List<Word> list = (List<Word>)request.getSession().getAttribute("list");
for (Word word : list) {
out.write("<tr>");
out.write("<td>"+word.getEnglish()+"</td>");
out.write("<td>"+word.getChinese()+"</td>");
out.write("</tr>");

}
%>
</table>
<script src="js/jquery-1.11.0.js"></script>
 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

5.场景化应用

1.可用于现代化教学与作业系统,提高课堂效率。
2.可用于模块化系统化练习,提升学习效率。
3.可用于竞赛与考试系统……

6.Demo

– 由于服务器资源昂贵,本项目暂不提供Demo

7.敬请期待(to do list)

1.实现单词限时测试(考试),后台为指定对象划定指定考试范围。
2.对单词分类,分词性,划分单元,划分学习阶段(高中,四级,六级)(数据库优化)。
3.创建单词练习功能,针对指定分类进行练习。
4.对每个用户记录每个单词错误的次数(数据库优化)
……


基于Java的英语单词测试系统
https://blog.funfs.com/project/projectx/
作者
Godtokoo
发布于
2023年4月25日
许可协议