首先是guess.jsp-----------
--------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<%@page import="holy.RandomNum"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>guess the number</title>
</head>
<body>
<jsp:useBean id="rand" class="holy.RandomNum" scope="session" />
<form id="guess" method="post" action="answer.jsp">
<input type="text" name="input" >
<input type="submit" value="guess it">
</form>
</body>
</html>
------------------------------------------
然后就是 answer.jsp-----------------------
-------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%
int ip=Integer.parseInt(request.getParameter("input"));
int ran=((Integer)request.getAttribute("rand")).intValue();
if(ip>ran)
{
out.print("sorry,too big");
}
else if(ip<ran)
{
out.print("sorry,too small");
}
else if(ip == ran)
{
out.print("conguratulations,you guess it!");
}
%>
</body>
</html>
--------------------------------------------------
还有一个javebean--------------
-----------------------------
package holy;
import java.util.Random;
public class RandomNum
{
private int random;
public RandomNum()
{
Random rand= new Random();
this.random =rand.nextInt(100);
}
public int getRandom() {
return random;
}
public void setRandom(int random) {
this.random = random;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + random;
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final RandomNum other = (RandomNum) obj;
if (random != other.random)
return false;
return true;
}
}
------解决方案--------------------
request.getParameter("input")
改为
request.getParameter("input") == null? "0":request.getParameter("input");