运用session保存数据
guess1.jsp
<body>
<a href="guess2.jsp">去玩猜数字游戏</a>
<%
int i=(int)(Math.random()*100+1);
session.setAttribute("number", 1);
session.setAttribute("count", 0);
%>
</body>
guess2.jsp
<body>
请输入你猜的数字:
<form action="guess3.jsp">
<input type="text" name="n">
<input type="submit" value="guess">
</form>
</body>
guess3.jsp
<body>
<%
String str=request.getParameter("n");
int number=Integer.parseInt(str);
int trueN=(Integer)session.getAttribute("number");
int count=(Integer)session.getAttribute("count");
count++;
session.setAttribute("count", count);
if(number>trueN){
out.print("猜大了,这是第"+count+"次,返回继续----》");
out.print("<a href='guess2.jsp'>=====</a>");
}else if(number>trueN){
out.print("猜小了,这是第"+count+"次,返回继续----》");
out.print("<a href='guess2.jsp'>=====</a>");
}else{
out.print("猜对了,一共花了"+count+"次");
}
%>
</body>
关注公众号:文经猫的诗和远方,获取博主最新的技术文章与人生感悟,共同成长,实现人生价值!