日期:2014-05-16  浏览次数:20398 次

jsp中session问题
下面程序是在页面中产生一个1--100之间的随机数利用session保存该数据,然后用户通过输入框输入1--100之间的数据与产生的随机数进行匹配,若输入数大于随机数、等于随机数或小于随机数都有相关的提示信息输出。程序有错请帮帮忙修改下:

cai.jap(主程序)

<%@ page contentType="text/html;charset=gb2312" %>
<html>
  <body>
  <P>随机给您分配了一个1到100之间的随机数,请猜猜!
  <% int number=(int)(Math.random()*100)+1;
  session.setAttribute("save",new Integer(number));
  %>
   
  <FORM action="restul.jsp" method=post name=form>
  <INPUT type="text" name="boy">
  <INPUT TYPE="submit" value="提交" name="submit">
  </FORM>
  </body>
  </html> 

restul.jsp(处理页面)
<%@ page contentType="text/html;charset=gb2312" %>

<html>
  <body>
  <% int m,n;
   
  String str=request.getParameter("boy");
  String str1=(String)session.getAttribute("save"); 
  m=Integer.parseInt(str1); //估计是这几行错啦,总是在这里报错
  n=Integer.parseInt(str);
   
   
  if(n==m){
  %>
  <jsp:forward page="success.jsp"/>//等于时转到此页面
  <%
  } else if (n > m) {
  %>
  <jsp:forward page="da.jsp" />//大于时时转到此页面
  <%
  } else {
  %>
  <jsp:forward page="xiao.jsp"/>//小于时时转到此页面
  <%
  }
  %>
  </body>
  </html>

da.jap(大于页面的提示)
<%@ page contentType="text/html;charset=gb2312" %>
<html>
  <body>
  <p>您输入的数比随机数大!请您重新在猜。
  <form action="restul.jsp">
  <input type="text" name="boy">
  <input type="submit" value="提交">
  </form>
  </body>
  </html>
   
xiao.jsp(小于页面的提示)
<%@ page contentType="text/html;charset=gb2312" %>
<html>
  <body>
  <p>您输入的数比随机数小!请您重新在猜。
  <form action="restul.jsp">
  <input type="text" name="boy">
  <input type="submit" value="提交">
  </form>
  </body>
  </html>
success.jsp(成功页面的提示)
<%@ page contentType="text/html;charset=gb2312" %>
  <html>
  <body>
  <p>恭喜您!你猜对啦。
  </p>
  </body>
  </html>

  帮帮忙咯,实在调试了很久都搞不定,谢谢啦!

------解决方案--------------------
cai.jap(主程序)

<%@ page contentType="text/html;charset=gb2312" %>
<html>
<body>
<P>随机给您分配了一个1到100之间的随机数,请猜猜!
<% int number=(int)(Math.random()*100)+1;
//session.setAttribute("save",new Integer(number)); 改为如下:
session.setAttribute("save",number+"");
%>

<FORM action="restul.jsp" method=post name=form>
<INPUT type="text" name="boy">
<INPUT TYPE="submit" value="提交" name="submit">
<