当前位置:编程学习 > JSP >>

谁有猜数字小游戏的JSP代码啊

答案:rand.jsp <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.io.*,javax.servlet.*"%> <html> <script language=javascript> function out(obj){ var i = obj ; if(i==0) document.location.href="judge.jsp"; i--; setTimeout("out("+i+")",1000); } </script> <body onload="out(0)"> <% StringBuffer str=new StringBuffer(); int i=1; do { long m=Math.round(Math.random()*9); int flag=str.indexOf(String.valueOf(m)); if(flag<=-1) { if(i==1) { str.append(m); } else{ str.append(m); } i++; } }while(i<5); session.setAttribute("strstr",str); %> <% String path=getServletContext().getRealPath("."); //out.println(path); File f=new File(path,"tem.txt"); //out.println(f); //out.println(f.exists()); if(f.exists()){//检查File.txt是否存在 f.delete();//删除File.txt文件 }else{ f.createNewFile();//在当前目录下建立一个名为File.txt的文件 } %> </body> </html> judge.jsp <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.io.*,javax.servlet.*"%> <html> <head> <title>数字</title> </head> <body> <%String userstr=request.getParameter("number"); int m=0; int n=0; StringBuffer str=(StringBuffer) session.getAttribute("strstr");%> <%if(userstr!=null&&str!=null){ for(int l=0;l<=3;l++){ for(int o=0;o<=3;o++){ if((userstr.substring(o,o+1)).equals(str.substring(l,l+1))){ if(o==l){m++;} else{n++;} } } } }%> <%if(userstr!=null){ String path=getServletContext().getRealPath("."); RandomAccessFile rf=new RandomAccessFile(path + "\\tem.txt","rw");//定义一个类RandomAccessFile的对象,并实例化 rf.seek(rf.length());//将指针移动到文件末尾 rf.writeBytes(userstr+" "+m+"A"+n+"B"+"<br>"); rf.close();//关闭文件流 out.println("写入文件内容为:<br>"); FileReader fr=new FileReader(path + "\\tem.txt"); BufferedReader br=new BufferedReader(fr);//读取文件的BufferedRead对象 String Line=br.readLine(); while(Line!=null){ out.println(Line + "<br>"); Line=br.readLine(); } fr.close();//关闭文件 }%> <form method="post" action="" onsubmit="return inputCheck()"> <input type="password" name="str" id="str" value=<%=str %>> <div><input type="text" name="number" id="number" maxlength="10" /> <span id="showtip" class="but_w5 txt_colorGray">请输入4位整数</span></div> <input type="submit" value="确定" /> <input type="reset" value="重填"/> <input type="button" value="开始" onclick="window.location='rand.jsp'"/> </form> <script type="text/javascript"> function inputCheck(){ var userstr = document.getElementById('number').value; var patrn = /^[0-9]\d{3}$/; //alert("go"); if(patrn.exec(userstr)){ //alert("go"); return ture; }else{ showtip.innerHTML = "<span class='red'>只能是四位整数 </span>"; return (false); } }</script> </body> </html> 一共两个jsp文件,你看看可以不。这是以前写的。

上一个:代码应该没问题,换了两个tomcat服务器,jsp文件就是说不能编译.下面是访问最简单的jsp的代码:
下一个:htm文件中可以写jsp代码吗

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,