用于生成随机数的CRandom.java文件为:
package mypro;
public class CRandom
{
private int initialNumber=0;
private int rand=0;
public CRandom()
{
}
public CRandom(int init)
{
setInitialNumber(init);
}
public void setInitialNumber(int num)
{
if(num<0)
initialNumber=0;
else
initialNumber=num;
generateRandom();
}
public void generateRandom()
{
rand=(int)(Math.random()*initialNumber)+1;
}
public int getRandom()
{
return rand;
}
}
用于保存提示信息的CInfo.java文件为:
package mypro;
public class CInfo
{
private String info;
public void setInfo(String str)
{
info=str;
}
public String getInfo()
{
return info;
}
}
用于猜数字的主页exec.jsp文件为:
<%@page c%>
<html>
<head>
</head>
<body>
<jsp:useBean id="cr" scope="session" class="mypro.CRandom"/>
<jsp:useBean id="ci" scope="session" class="mypro.CInfo"/>
<%
if(session.isNew())
{
ci.setInfo("Hello!");
cr.setInitialNumber(100);
//out.print(cr.getRandom());
}
%>
<jsp:getProperty name="ci" property="info"/><br>
<form method="POST" action="action.jsp">
<p>猜数字:<input type="text" name="guessNumber" size="20"></p>
<input type="submit" value="登录" name="B1">
</form>
</body>
</html>
用于判断并且实现分发跳转的action.jsp文件为:
<%
String s1=request.getParameter("guessNumber");
int num1=Integer.parseInt(s1);
mypro.CRandom cr=(mypro.CRandom)session.getAttribute("cr");
int num2=cr.getRandom();
mypro.CInfo ci=(mypro.CInfo)session.getAttribute("ci");
if(num1==num2)
{
out.print("OK!<p><a href='exec.jsp'>Please click this to continue...</a>");
session.invalidate();
}
else if(num1<num2)
{
ci.setInfo("lower");
RequestDispatcher dispatcher = request.getRequestDispatcher("exec.jsp");
dispatcher.forward(request, response);
}
else
{
ci.setInfo("higher");
RequestDispatcher dispatcher = request.getRequestDispatcher("exec.jsp");
dispatcher.forward(request, response);
}
%>
[此贴子已经被作者于2010-12-12 08:16:59编辑过]