[JavaWeb]利用session简单做一个在线考试系统

题目

要求显示班级和用户名用 session

e46beb0be8db40c3a5e09fd1abe2ab1e.jpeg

解题代码

页面1: login.jsp

3a9fc74c250346c695d469452a1ce50d.jpeg

<%@ 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>
<body>

<form method="POST" action="test.jsp">
<p>选择班级:
	<select size="1" name="cls">
		<option value="1班" selected>1班</option>
		<option value="2班">2班</option>
		<option value="3班">3班</option>
	</select>
</p>
<p>输入用户名:</p>
<input type="text" name="username">
<input type="submit" value="登录">
</form>

</body>
</html>

页面2: test.jsp

1885386ef64a45bd9f4139d4d8d1ae47.jpeg

<%@ 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>
<body>
<%
request.setCharacterEncoding("utf-8");
	String cls = request.getParameter("cls");
	session.setAttribute("cls", cls);	
	String username = request.getParameter("username");
	session.setAttribute("username", username);

	if(username.equals("")){
		response.sendRedirect("login.jsp");
	}
	else{
		out.println("欢迎登录 "+username+"<br>"+"下面开始考试!");
	}
		
%>

<form method="POST" action="result.jsp">
<p>1.填空题:</p>
2+2=?
	<input type="text" name="question1">
<p>2.单选题:</p>
<p>十进制数 12 转换成二进制是多少?</p>
	<input type="radio" name="question2" value="A">A.1100
	<input type="radio" name="question2" value="B">B.1010
	<input type="radio" name="question2" value="C">C.1101
<p>3.多选题:</p>
<p>下面哪些选项是正确?</p>
	<input type="checkbox" name="question3" value="A">A.1+1=2
	<input type="checkbox" name="question3" value="B">B.1*0=1
	<input type="checkbox" name="question3" value="C">C.2/2=1
<p></p>
<input type="submit" value="提交">
</form>

</body>
</html>

页面3: result.jsp

102e01b4853248198d63c3ad46a1fa65.jpeg

<%@ 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>
<body>
<%
request.setCharacterEncoding("utf-8");	
	String question1 = request.getParameter("question1");
	String question2 = request.getParameter("question2");
	String question3[] = request.getParameterValues("question3");
	
	String 班级=(String)session.getAttribute("cls");
	String 用户名=(String)session.getAttribute("username");	
%>

<p>班级:<%=班级 %></p>
<p>用户名:<%=用户名 %></p>
<p>提交的答案:1.<%=question1 %> 2.<%=question2 %> 3.
<%
	if (question3 != null){	
		for(int i=0;i<question3.length;i++){ // 输出复选框数组的值
%>
			<%=question3[i] %>
<%
		} 
	}
%>
<p>
<p>标准答案:1.4 2.A 3.A C</p>
<%
	int score=0;
	
	if(question1.equals("4")){
		score += 2;
	}
	if(question2.equals("A")){
		score += 2;
	}
	if(question3 != null){
		if(question3[0].equals("A") && question3[1].equals("C")){ // 全答对
			score += 2;
		}
		else if(question3[0].equals("A") && question3[1].equals("C")){ // 答对一个
			score += 2;
		}
	}
%>

<p>得分:<%=score %>分</p>

</body>
</html>

运行结果

57276b68099c480c82b21818b682e069.jpeg

76f0910e88a245b184658a3daa5c0b21.jpeg

49fb303ca0d64905ad7015d1a8a445b9.jpeg

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lin_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值