为了账号安全,请及时绑定邮箱和手机立即绑定

猜数字问题

package test1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;

/**
 * Servlet implementation class number2
 */
@WebServlet("/doGuess")
public class number2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public number2() {
        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.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		int max=20;
		int min=0;
		Random random=new Random();
		int s=random.nextInt(max)%(max-min+1)+min;
		HttpSession session=request.getSession();
		session.setAttribute("s", s);
		session.getAttribute("s");
		String number=request.getParameter("number");
		int numbe=Integer.parseInt(number);
		ServletContext context=request.getServletContext();
		Integer str=(Integer)context.getAttribute("s");
		if(str==numbe){
			out.append("猜对了!正确的数字是:"+str);
		}
		else if(str<numbe){
			out.append("猜大了");
		}
		else{
			out.append("猜小了");
		}
		out.close();
	}

	/**
	 * @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);
	}

}

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>猜数字游戏</title>
<meta name="generator" content="Bootply" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style type="text/css">
.modal-footer {   border-top: 0px; }
.form-control-half {
    width: 60%;
    height: 34px;
    padding: 6px 12px;
    font-size: 14px;
    line-height: 1.42857143;
    color: #555;
    background-color: #fff;
    background-image: none;
    border: 1px solid #ccc;
    border-radius: 4px;
    -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
    box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
    -webkit-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
</style>
</head>
<body>
<!--login modal-->
<div id="loginModal" class="modal show" tabindex="-1" role="dialog" aria-hidden="true">
  <div>
  <div>
      <div>
          <button type="button" data-dismiss="modal" aria-hidden="true">×</button>
          <h1>猜数字</h1>
      </div>
      <div>
          <form class="form col-md-12 center-block" action="doGuess" method="post">
            <div>
              <input type="text" name="number" class="form-control input-lg" placeholder="猜一个0~20之间的整数">
            </div>
         <div class="btn-group pull-right">
         
          <button class="submit btn btn-primary">手气如何</button>
 </div>
          </form>
      </div>
      <div>
        
      </div>
  </div>
  </div>
</div>
<!-- script references -->
<script type='text/javascript' src="js/jquery-1.9.1.min.js"></script>
        <script type='text/javascript' src="js/bootstrap.min.js"></script>

</body>
</html>
求高手指点,错在哪里了

正在回答

举报

0/150
提交
取消
JAVA遇见HTML——Servlet篇
  • 参与学习       160615    人
  • 解答问题       1029    个

本门课程在JSP课程的基础上,深入介绍Servlet的基础知识

进入课程

猜数字问题

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信