<%@ Page Language="Java"Import="java.util.*"pageencoding="Utf-8"%><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML> <Head> <Basehref= "<%=basePath%>"> <title>My JSP ' game.jsp ' starting page</title> <Metahttp-equiv= "Pragma"content= "No-cache"> <Metahttp-equiv= "Cache-control"content= "No-cache"> <Metahttp-equiv= "Expires"content= "0"> <Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3"> <Metahttp-equiv= "description"content= "This is my page"> <!--<link rel= "stylesheet" type= "Text/css " href= "Styles.css" > - </Head> <Body> <%!intCount= 0;%> <% Objectobj=Session.getattribute ("Numint");//when the first visit is NULL, there is a value after the second accessif(obj== NULL) {Random R= NewRandom (); Obj=R.nextint ( -); Session.setattribute ("Numint", obj); } intRM= Integer. parseint (Obj.tostring ()); %> <% StringNumstr=Request.getparameter ("Num");//get the number of guessesintcou=0; if(numstr! )= NULL && !"". Equals (NUMSTR)) {//Direct Browser URL Access Jsp,num value is null, when committed, no value for NUM is entered, num value is"" //num must have a value of Count++; Cou=count; intNum= Integer. parseint (NUMSTR); if(Num>RM) {Out.println ("it's too big."); } Else if(Num<RM) {Out.println ("it's too small."); } Else{out.println ("finally guessed it right."); Random R= NewRandom (); Obj=R.nextint ( -); Session.setattribute ("Numint", obj); Count=0; } } %>you are welcome to play the game of guessing, guess<%=cou%>Times<%--<%out.print (RM);--%> <formAction= "game.jsp"Method= "POST"> <BR>Enter a number<inputname= "num"> <inputtype= "Submit"value= "Click to confirm"> </form> </Body></HTML>
Run:
jsp-a simple guess little game