编写一道图形记忆测试试题,掌握session bean存储用户的数据 步骤如下 (1)编写JavaBean类 Memory.java用于构建模型 (2)编写choiceGrage. jsp, 该页面中的form 表单中使用radio 标记选择记忆测试级别: 初级、中级和高级。初级需要记忆一个长度为5 个字符的字符序列(例如★■★▲●) , 中级需要记忆一个长度为7 个字符的字符序列,高级需耍记忆一个长度为10 个字符的字符序列。在choiceGrage.jsp 页面选择级别后,单击form 表单的提交键提交给giveTest. jsp 页面。 (3)编写giveTest. jsp 页面,该页面获取choiceGrage. jsp 贞面提交的级别后.使用session bean 显示testString 的屈性值(例如展性值是长度为7 个字符的字符序列),然后提示用户在5 秒内记住这个字符序列。5 秒后,该页面将自动定向到answerTest.jsp 页面。 (4)编写answerTest. jsp 页面, 该页面的form 表单提供用户给出答案的界面,即使用radio 标记让用户选择字符序列中的各个字符,以此代表用户认为自己记住的字符序列。单击提交键. 将选择提交给judgeAnswer. jsp 页面 (5)编写judgeAnswer. jsp 页面,该页面负责判断有户是否记住了giveTest. jsp 页面给出 的字符序列。
时间: 2023-06-20 12:07:27 浏览: 126
(1) Memory.java
```java
public class Memory implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String testString;
public Memory() {
}
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
}
```
(2) choiceGrage.jsp
```jsp
<html>
<head>
<title>选择级别</title>
</head>
<body>
<h2>请选择级别</h2>
<form action="giveTest.jsp" method="post">
<input type="radio" name="grade" value="1">初级<br>
<input type="radio" name="grade" value="2">中级<br>
<input type="radio" name="grade" value="3">高级<br>
<input type="submit" value="开始测试">
</form>
</body>
</html>
```
(3) giveTest.jsp
```jsp
<%@ page import="javax.servlet.http.HttpSession" %>
<%@ page import="java.util.Random" %>
<%
HttpSession session = request.getSession(true);
Memory memory = (Memory) session.getAttribute("memory");
if (memory == null) {
memory = new Memory();
session.setAttribute("memory", memory);
}
int grade = Integer.parseInt(request.getParameter("grade"));
int length = 0;
String chars = "";
switch (grade) {
case 1:
length = 5;
chars = "★■★▲●";
break;
case 2:
length = 7;
chars = "★■★▲●◆♦";
break;
case 3:
length = 10;
chars = "★■★▲●◆♦□■▲●";
break;
}
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(chars.charAt(random.nextInt(chars.length())));
}
memory.setTestString(sb.toString());
response.setHeader("Refresh", "5; URL=answerTest.jsp");
%>
<html>
<head>
<title>记忆测试</title>
</head>
<body>
<h2>请记住以下字符序列</h2>
<p><%= memory.getTestString() %></p>
<p>这个字符序列将在5秒钟后消失,请在5秒内记住它</p>
</body>
</html>
```
(4) answerTest.jsp
```jsp
<%@ page import="javax.servlet.http.HttpSession" %>
<%@ page import="java.util.Arrays" %>
<%
HttpSession session = request.getSession(false);
if (session == null) {
response.sendRedirect("choiceGrage.jsp");
return;
}
Memory memory = (Memory) session.getAttribute("memory");
if (memory == null) {
response.sendRedirect("choiceGrage.jsp");
return;
}
%>
<html>
<head>
<title>记忆测试</title>
</head>
<body>
<h2>请在下面选择你记住的字符序列</h2>
<form action="judgeAnswer.jsp" method="post">
<% String testString = memory.getTestString();
char[] chars = testString.toCharArray();
Arrays.sort(chars);
for (char c : chars) { %>
<input type="radio" name="answer" value="<%= c %>"><%= c %>
<% } %>
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
```
(5) judgeAnswer.jsp
```jsp
<%@ page import="javax.servlet.http.HttpSession" %>
<%
HttpSession session = request.getSession(false);
if (session == null) {
response.sendRedirect("choiceGrage.jsp");
return;
}
Memory memory = (Memory) session.getAttribute("memory");
if (memory == null) {
response.sendRedirect("choiceGrage.jsp");
return;
}
String answer = request.getParameter("answer");
if (answer == null || answer.length() != memory.getTestString().length()) {
out.println("<h2>答案错误</h2>");
} else {
char[] correct = memory.getTestString().toCharArray();
Arrays.sort(correct);
char[] userAnswer = answer.toCharArray();
Arrays.sort(userAnswer);
if (Arrays.equals(correct, userAnswer)) {
out.println("<h2>答案正确</h2>");
} else {
out.println("<h2>答案错误</h2>");
}
}
session.removeAttribute("memory");
%>
```
阅读全文