一、实验目的
掌握怎样在JSP中使用内置对象session
二、实验项目内容(实验题目)
编写代码,掌握session的用法。【参考课本4.6.5 】
三、源代码以及执行结果截图:
choiceGrade.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body bgcolor = #ffccff>
<style>
#textStyle{
font-family:宋体; font-size:26;color:bule
}
</style>
<form action="giveTest.jsp" id="textStyle" method=post name=form>
<input type=radio name="grade"value="5"/>初级
<input type=radio name="grade"value="7"checked="OK"/>中级
<input type=radio name="grade"value="10"/>高级
<br><input type="submit"name="submit"id="textStyle"value="提交"/>
<input type="reset" id="textStyle"value="重置"/>
</form>
</body>
</html>
giveTest.jsp
<%@ page language="java" contentType="text/html; utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Random" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body bgcolor = #ffccff>
<style>
#textStyle{
font-family:宋体; font-size:26;color:bule
}
</style>
<%! static ArrayList<String>list=new ArrayList<String>();
static{
list.add("☆");
list.add("⚪");
list.add("🔺");
list.add("☐");
list.add("◇");
}
String getNextTestString(int length){
StringBuffer buffer=new StringBuffer();
Random random=new Random();
for(int i=0;i< length;i++){
int index=random.nextInt(list.size());
String str=list.get(index);
buffer.append(str);
}
return new String(buffer);
}
%>
<%
String grade = request.getParameter("grade");
if(grade == null){
grade = (String) session.getAttribute ("grade");
}
int number = Integer.parseInt(grade);
session.setAttribute("grade",grade);
String testString= null;
String yesORNo = null;
yesORNo= (String)session.getAttribute("yes0RNo");
if(yesORNo == null) {
testString = getNextTestString(number) ;
session.setAttribute ("yes0RNo","yes");
session.setAttribute("testString",testString);
}
else if(yesORNo. equals("yes")){
response. sendRedirect("answerTest.jsp");
return;
}
else if(yesORNo. equals("no")){
testString = getNextTestString (number);
session. setAttribute (" yes0RNo" ,"yes");
session. setAttribute ("testString",testString);
}
%>
<p>
id="textStyle">给5秒记住您看到的字符序列:<br>
<%=testString %>
<br>5秒后,将转到答题页.
<% response.setHeader(" refresh","5");
%>
</p>
</body>
</html>
answerTest.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body bgcolor = #ffccff>
<style>
#textStyle{
font-family:宋体; font-size:26;color:bule
}
</style>
<form action="judgeAnswer.jsp"id="textStyle" method=post>
<%
int n=Integer.parseInt((String)session.getAttribute("grade"));
session.setAttribute("yesORNo","no");
for(int i=1;i<=n;i++){
out.print("<br>第"+i+"个字符:");
out.print("<input type=radio name=R"+i+" value='☆'/>☆"+
"<input type=radio name=R"+i+" value='⚪'/>⚪"+
"<input type=radio name=R"+i+" value='🔺'/>🔺"+
"<input type=radio name=R"+i+" value='☐'/>☐"+
"<input type=radio name=R"+i+" value='◇'/>◇");
}
%>
<br><input type="submit" name="submit" id="textStyle" value="提交" />
<input type="reset" id="textStyle" value="重置" />
</form>
</body>
</html>
judgeAnswer.jsp
<%@ page language="java" contentType="text/html;utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body bgcolor =while>
<p style="font-family:宋体; font-size:26;color:bule">
<%
session.setAttribute("yesORNo","no");
request.setCharacterEncoding("utf-8");
int n=Integer.parseInt((String)session.getAttribute("grade"));
StringBuffer buffer=new StringBuffer();
for(int i=1;i<=n;i++){
buffer.append(request.getParameter("R"+i));
out.print(""+request.getParameter("R"+i));
}
String userAnswer=new String(buffer);
String testString=(String)session.getAttribute("testString");
if(testString.equals(userAnswer)){
out.print("<br>您记忆还不错也!");
}
else{
out.print("你戳啦!答案是:<br>"+testString);
}
%>
<br><a href="giveTest.jsp">返回,回去练习</a>
<br><br>
<a href="choiceGrade.jsp">重新选择级别</a>
</p>
</body>
</html>
效果图