编写5个jsp页面inputGuess.jsp,result.jsp,small.jsp,large.jsp和success.jsp,实现猜数字游戏。具体要求如下: 1) inputGuess.jsp的具体要求 用户请求inputGuess.jsp时,随机分配给该用户一个1到100之间的数字,该页面同时负责将这个数字保存在用户的session对象中。该页面提供表单,用户可以使用该表单输入自己的猜测,并提交给result.jsp页面。 2) result.jsp的具体要求 该页面负责判断inputGuess.jsp提交的猜测是否和用户的session对象中存放的那个数字相同,如果相同就将用户重定向到success.jsp;如果不相同就将用户重定向到large.jsp或small.jsp。 3) small.jsp和large.jsp的具体要求 small.jsp和large.jsp提供表单,用户可以使用该表单继续输入自己的猜测,并提交给result.jsp页面。 4) success.jsp的具体要求 该页面负责显示用户成功的消息,并负责输出用户session对象中的数据。并且还要求输出猜中数字所用的次数。
inputGuess.jsp
<%@ page import="java.util.Random" %>
<%@ page import="com.sun.javafx.scene.traversal.Hueristic2D" %><%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/4/5
Time: 16:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="result.jsp" method="post">
请输入您的猜测:
<input type="text" name="guess"><br>
<input type="submit">
</form>
<%
Random r = new Random();
int randomNumber = r.nextInt(100);
// 在session传入随机数
session.setAttribute("randomNum", randomNumber);
// session中再存入一个数count 用来计数
int count = 0;
session.setAttribute("count", count);
%>
</body>
</html>
result.jsp
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/4/5
Time: 16:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
// 取出session中随机的数
int randomNumber = (int) session.getAttribute("randomNum");
// 取出提交的 自己猜测的数
int guessNumber = Integer.parseInt(request.getParameter("guess"));
// 如果相同 success.jsp 如果不同 large.jsp small.jsp
if (guessNumber == randomNumber) {
int count = (int) session.getAttribute("count");
count++;
session.setAttribute("count", count);
response.sendRedirect("success.jsp");
} else if (guessNumber > randomNumber) {
int count = (int) session.getAttribute("count");
count++;
session.setAttribute("count", count);
response.sendRedirect("large.jsp");
} else if (guessNumber < randomNumber) {
int count = (int) session.getAttribute("count");
count++;
session.setAttribute("count", count);
response.sendRedirect("small.jsp");
}
%>
</body>
</html>
large.jsp
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/4/5
Time: 16:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="result.jsp" method="post">
猜大了,再猜
<input type="text" name="guess"><br>
<input type="submit">
</form>
</body>
</html>
small.jsp
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/4/5
Time: 16:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="result.jsp" method="post">
猜小了,再猜
<input type="text" name="guess"><br>
<input type="submit">
</form>
</body>
</html>
success.jsp
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/4/5
Time: 17:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
恭喜您,猜对了<br>
正确的数字是:<%=session.getAttribute("randomNum")%><br>
您一共猜了<%=session.getAttribute("count")%>次
</body>
</html>