精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

jsp+servlet實現猜數字游戲

jsp+servlet實現猜數字游戲主要用到了servlet方面的session,httpservletresponse類中的sendredirect()方法。

step1,客戶訪問或刷新getnumber.jsp頁面時,隨機給用戶分配一個1-100之間的數字,將該數字存放到客戶的session中。

<%@ page language="java" import="java.util.*" 
contenttype="text/html;charset=utf-8"%>
<html>
 <head> 
 </head> 
 <body>
 <%
 string message=(string)session.getattribute("message");
 %>
 <form action="handleguess" methods="post">
  <input type="text" name="guessnumber" size=8>
  <%=message %>
  <input type="submit" value="提交">
  </form>
  <form action="getnumber.jsp" method="post">
  <input type="submit" value="重新得到新的隨機數">
  </form>
 </body>
</html>

step2,然后用戶超廉價到inputnumber.jsp頁面上,用戶輸入自己的猜測,并提交給handleguess類負責創建。

<%@ page language="java" import="java.util.*" pageencoding="utf-8" 
contenttype="text/html;charset=utf-8"%>
<html>
 <head>
 </head> 
 <body>
 <% session.setattribute("message", "請您猜數");
 // httpsession session =request.getsession(true);
 int randomnumber=(int)(math.random()*100)+1;//獲取一個隨機數
 session.setattribute("savenumber",new integer(randomnumber));
 %>
 <p>訪問或刷新該頁面可以隨機跌倒一個1,到100之間的數字
 <br>單擊下面鏈接可以訪問去猜數字
 <a href="inputnumber.jsp" rel="external nofollow" >去猜數字</a>
 </body>
</html>

step3,該servlet類為:

import java.io.ioexception;
import java.io.printwriter;
 
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
 
public class handleguess extends httpservlet { 
 public void dopost(httpservletrequest request,httpservletresponse response)
 throws servletexception,ioexception{
 request.setcharacterencoding("utf-8");
 response.setcontenttype("text/html;charset=utf-8");
 printwriter out=response.getwriter(); 
 //獲取客戶猜測提交的數
 int guessnumber=integer.parseint(request.getparameter("guessnumber"));
 //獲取客戶的會話對象
 httpsession session=request.getsession();
 integer integer=(integer)session.getattribute("savenumber");
 int realnumber=integer.intvalue();
 system.out.println(realnumber);
 system.out.println(guessnumber);
 if(guessnumber>realnumber){
 session.setattribute("message", "您猜大了");
 system.out.println("您猜大了---no");
 response.sendredirect("inputnumber.jsp");
 }
 else if(guessnumber<realnumber){
 session.setattribute("message", "您猜小了");
 system.out.println("您猜小了---no");
 response.sendredirect("inputnumber.jsp");
 }
 else{
 session.setattribute("message", "您猜對了");
 system.out.println("您猜對了");
 response.sendredirect("inputnumber.jsp");
 }
 }
 public void doget(httpservletrequest request,httpservletresponse response)
  throws servletexception,ioexception{
 dopost(request,response);
 }
 
}

step4,web.xml配置,省略。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持碩編程。

相關文章