编写servlet,产生一个1-10的随机数,将其保存到session中,然后重定向到输入页面sendMsg.jsp,在此页面输入所猜测的数后,提交到另一个servlet判断是否正确,若不正确,转发到sendMsg.jsp,在此页面显示“大了”或“小了”的提示信息,若正确,则在新页面显示“答对了”。(网页版的猜数游戏)
首先创建FirstServlet,该为产生的随机数的。代码如下所示:
package Servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FirstServlet extends HttpServlet {
public void init(ServletConfig config)throws ServletException {
super.init();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
int msg = Integer.parseInt(request.getParameter("msg"));
//float msg = Float.parseFloat(request.getParameter("msg"));
HttpSession session = request.getSession();
int num = (int) session.getAttribute("num");
if (msg == num){
response.sendRedirect("printfMsg.jsp");
}else {
String m = msg > num ? "大了" : "小了";
request.setAttribute("m",m );
request.getRequestDispatcher("sendMsg.jsp").forward(request,response);
}
}
}
接下来创建PutServlet,该为判断是否正确,代码如下所示:
package Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;
public class PutServlet extends HttpServlet {
public void init(ServletConfig config)throws ServletException {
super.init();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Random random = new Random();
int num = random.nextInt(10)+1;
System.out.println(num);
HttpSession session = request.getSession();
session.setAttribute("num", num); //将用户名保存到session
System.out.println(num);
response.sendRedirect("sendMsg.jsp"); //重定向到sendMsg.jsp
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
接下来编写页面sendMsg,代码如下所示:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="ser" method="post">
<input type="text" name="msg">
<input type="submit"><br>
<%-- 该为接收大了 还是 小了--%>
${m}
</form>
</body>
</html>
紧接着编写更新了猜对了页面,代码如下所示:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>恭喜你猜对了~</h1>
${msg}
</body>
</html>
最后编写web.xml文件,代码如下所示:
<web-app>
<servlet>
<servlet-name>de</servlet-name>
<servlet-class>Servlet.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>de</servlet-name>
<url-pattern>/ser</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dei</servlet-name>
<servlet-class>Servlet.PutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dei</servlet-name>
<url-pattern>/put</url-pattern>
</servlet-mapping>
</web-app>
最后的运行效果先输入put回车,然后它会跳转到sendMsg页面,直到输入正确的数字才会跳转到猜对了的页面,效果图如下图所示:
当我输入9时,提示词为大了,
知道输入正确的数字后跳转到猜对了的页面