<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
<center>
<h2>在线测试系统</h2>
<p>
<font color="#4b5cc4">冷静思考,认真答题</font>
|
<a href="scorelist.jsp">成绩榜</a>
</p>
<form name="form1" method="get">
用户名:<input type="text" name="username" />
<br>
<br>
密    码:<input type="password" name="psd" />
<br>
<br>
<input type="button" value="登录" onclick="document.form1.action='check.jsp';document.form1.submit()">
    
<input type="button" value="注册" onclick="document.form1.action='register.jsp';document.form1.submit()">
<%!int pageCount = 0;%>
<% pageCount++; %>
<p>Copyright @ 2019 | 访问次数:<%=pageCount%></p>
<h5><font color="red" >如果已有账号,请直接登录;如果没有账号,则填入信息后直接点击注册</font></h5>
</center>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!--写一个java bean来存储你的数据库信息,下面之间调用就好了-->
<jsp:useBean id="mysql" class="beans.SQL_login" scope="page"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<center>
<%
//获取用户名和密码
String uname=request.getParameter("username");
String pwd=request.getParameter("psd");
if(uname!=null && pwd!=null && uname!="" && pwd!=""){
//连接数据库,查找用户名和密码
Connection conn= null;
conn=mysql.getConnection();
Statement stmt=conn.createStatement();
String sql="INSERT INTO login VALUES('"+uname+"','"+pwd+"')";
int re=stmt.executeUpdate(sql);
//处理结果
if(re==1){
out.print("注册成功!点击");
%>
<a href="Index.jsp">此处</a>
<%
out.print("返回首页");
}else{
%>
<a href="Index.jsp">注册失败!请重新注册</a>
<%
}
}else{
%>
<a href="Index.jsp">用户名或密码不能为空!请重新注册</a>
<%
}
%>
</center>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="mysql" class="beans.SQL_login" scope="page"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<center>
<%
ResultSet re = null;
//获取用户名和密码
request.setCharacterEncoding("UTF-8");
String uname=request.getParameter("username");
String pwd=request.getParameter("psd");
//连接数据库,查找用户名和密码
Connection conn= null;
conn=mysql.getConnection();
Statement stmt=conn.createStatement();
String sql="SELECT * FROM login WHERE username='"+uname+"' and password='"+pwd+"'";
re=stmt.executeQuery(sql);
//处理结果
if(re!=null && re.next()){
session.setAttribute("username",uname);
%>
<h2>欢迎<%=uname %><a href='test.jsp'>开始测试</a></h2>
<%
}else{
%>
<h2>请先注册!3秒后返回首页...</h2>
<%
response.setHeader("refresh", "3;url='Index.jsp'");
}
//关闭连接
mysql.closeConnection(conn);
%>
</center>
</body>
</html>
用题库随机选取5道题,首先你需要一个题库
这里给大家提供一个博主总结的一个java题库
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="mysql" class="beans.SQL_login" scope="page"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在线考试</title>
</head>
<body>
<%
String username=(String)session.getAttribute("username");
if(username==null){
out.print("未登陆,3秒钟后重新登录");
response.setHeader("refresh", "3;url='Index.jsp'");
}
else{
%>
考生:<%=session.getAttribute("username") %>
<center>
<h3>在线测试题</h3>
<form action="submit.jsp" onsubmit="return confirm('确定提交吗?')" method="get">
<%
int i=0;
Connection conn= null;
conn=mysql.getConnection();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//随机出5道题
String sql="SELECT * FROM test_topic ORDER BY RAND() LIMIT 5";
ResultSet rs=stmt.executeQuery(sql);%>
<table>
<%
while(rs.next()){ %>
<tr><!-- 获取题目和选项 -->
<td><%=++i%>.<%=rs.getString("topic")%><%=rs.getString("currect") %><br/>
<input type="radio" value="A" name="q<%=i %>"><%=rs.getString("A")%><br/>
<input type="radio" value="B" name="q<%=i %>"><%=rs.getString("B")%><br/>
<input type="radio" value="C" name="q<%=i %>"><%=rs.getString("C")%><br/>
<input type="radio" value="D" name="q<%=i %>"><%=rs.getString("D")%><br/><br/></td>
<input type="hidden" name="id<%=i %>" value="<%=rs.getString("topicID")%>">
</tr>
<%}
}%>
</table>
<button type="submit">提交</button>
</center>
</form>
</body>
</html>
用列表存储用户的选项和所选的题目
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<jsp:useBean id="mysql" class="beans.SQL_login" scope="page"/>
<!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>
<body>
<%
String username=(String)session.getAttribute("username");
if(username==null){
out.print("未登陆,3秒钟后重新登录");
response.setHeader("refresh", "3;url='Index.jsp'");
}
else{
%>
<h2>考生:<%=session.getAttribute("username") %><h2>
<%
}
int score=0;
Connection con= mysql.getConnection();
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//将题号插入列表
String id1=request.getParameter("id1");
String id2=request.getParameter("id2");
String id3=request.getParameter("id3");
String id4=request.getParameter("id4");
String id5=request.getParameter("id5");
List<String> da=new ArrayList();
da.add(id1);
da.add(id2);
da.add(id3);
da.add(id4);
da.add(id5);
//选取选项
String q1=request.getParameter("q1");
String q2=request.getParameter("q2");
String q3=request.getParameter("q3");
String q4=request.getParameter("q4");
String q5=request.getParameter("q5");
List<String> zq=new ArrayList();
zq.add(q1);
zq.add(q2);
zq.add(q3);
zq.add(q4);
zq.add(q5);
//提取正确答案
for(int i=0;i<da.size();i++){
if(zq.get(i)!=null){
int a=Integer.parseInt(da.get(i));//遍历题号
String b=zq.get(i);//遍历选项
String sql="select currect from test_topic where topicID='"+a+"'";//查找题号所代表的题
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
if(b.equals(rs.getString("currect"))){//选项和答案比较
score+=20;
}
}
}else{
score+=0;
}
}
//将成绩存入成绩榜
String sql1="INSERT INTO scorelist VALUES('"+username+"','"+score+"')";
stmt.executeUpdate(sql1);
%>
<center>
<h2>你的成绩是:<%=score %></h2></br>
查看<a href="scorelist.jsp">成绩榜</a>
</center>
</body>
</html>
可以查看所有用户的成绩
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="mysql" class="beans.SQL_login" scope="page"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成绩榜</title>
</head>
<body>
<%
Connection conn= null;
conn=mysql.getConnection();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql="select * from scorelist";
ResultSet rs=stmt.executeQuery(sql);
rs.last();
%>
<center>
<h2>成绩榜</h2>
<table border="2" bgcolor= "CCCEEE" width="600">
<tr bgcolor="CCCCCC" align="center">
<th>编号</th>
<th>姓名</th>
<th>成绩</th>
</tr>
<%
rs.beforeFirst(); //移至第一条记录之前
while(rs.next()){
%>
<tr align="center">
<td><%= rs.getRow()%></td>
<td><%= rs.getString("username")%> </td>
<td><%= rs.getString("score")%> </td>
</tr>
<%}
mysql.closeConnection(conn);
%>
</table>
<a href="Index.jsp">返回首页</a>
</center>
</body>
</html>
以上就是这个简单的在线考试系统,只实现了基本的功能。
还有一个java bean表示数据库的页面未给大家展示出来,嘿嘿,加油哟~~~
本次的框架参考了以下这位博主的博客
非常感谢这位博主