JSP第六次作业:使用servlet实现记录次数猜数游戏

题目:

没有题目,第三次猜数了,想怎么写怎么写吧。

没有想法,上课没有听讲缘故。

1.相对路径不会写...这个可以把文件放在一个目录下应付。

2.不知道servlet里可不可以写表单,还是只能被传参...这个再写一个跳转页面应付。

综上,考查课最后一次作业,我真的就是随便应付的实现了。//不过有时间一定会搞明白加赘述的!

页面1:input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form name="f1" method="post" action="solve">
		The random number is generated successfully! 
		<br><br>
		Range [1,100], now start guessing this number:
		<br><br>
		<input type="text" name="ans"> 
		<input type="submit" name="sub" value="确定">
	</form>
</body>
</html>

  

servlet:solve.java

package anyi;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.Math;

/**
 * Servlet implementation class solve
 */
@WebServlet("/solve")
public class solve extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public int num = (int) (Math.random() * 100) + 1;  //生成随机数
    public int i = 0;//记录猜测次数
    /**
     * @see HttpServlet#HttpServlet()
     */
    public solve() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		response.setCharacterEncoding("utf-8");
		int ans=Integer.parseInt(request.getParameter("ans"));
		PrintWriter pw = response.getWriter();
        pw.print("<html><body><br>");
        if (ans == num) {
        	i++;
        	pw.print("The correct answer is "+num);
            pw.print("<br><br>Congratulations, you guessed it right!<br><br>"+"You guessed "+i+" times in total!");
            pw.print("<br><br><a href=\"input.jsp\">Play again</a>");
            num = (int) (Math.random() * 100) + 1;  //重置随机数
        	i = 0;//重置猜测次数
        } else if (ans > num) {
        	i++;
            pw.print("Sorry,You guess the big!<br><br>");
            pw.print("<a href=\"try again.jsp\">Please try again</a>");
        }else {
        	i++;
        	pw.print("Sorry,You guess the small!");
        	pw.print("<a href=\"try again.jsp\">Please try again</a>");
        }
        pw.print("</body></html>");
		
	}

}

  

页面2:try again.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <br>
	<form name="f1" method="post" action="solve">
		try again: <input type="text" name="ans"> 
		<input type="submit" name="sub" value="确定">
	</form>
</body>
</html>

  

posted @ 2021-05-28 16:19  anyiya  阅读(528)  评论(0编辑  收藏  举报