利用jsp/servlet/Ajax,实现一个猜数字游戏!
来源:互联网 发布:国外二维码扫描软件 编辑:程序博客网 时间:2024/04/30 06:37
源码:
http://download.csdn.net/detail/m1872216/4651239
Ajax的局部刷新:
input.jsp
<body>
<div id="msg" style="color:red"></div>
<form action="login.do" method="post" name="form1" onsubmit="return onClick()">
<table border="1">
<tr>
<td>
设置数字:
</td>
<td>
<input type="text" name="num"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
<script type="text/javascript" src="js/myAjax.js"></script>
<script type="text/javascript">
function onClick(){
var num=document.form1.num.value;
var params="num="+num;
ajaxPost("login.do",params,function(txt){
document.getElementById("msg").innerHTML=txt;
});
return false;
}
</script>
</body>
servlet:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//String count=request.getParameter("count");
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
//访问的次数
if(session.getAttribute("times")==null){
Random rd=new Random();
int count=rd.nextInt(10);
System.out.println(count);
session.setAttribute("count", count);
session.setAttribute("times", 0);
}
///////////////////////////////////////////////////////////////
if((Integer)session.getAttribute("times")==0){
int times=(Integer)session.getAttribute("times");
session.setAttribute("times", times+1);//初次访问
showResult(request,response);
}else{
showResult(request,response);
}
}
public void showResult(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
//获取访问的第几次
int times=(Integer)session.getAttribute("times");
//获取客户端提交的数据
int num=0;
try{
num=Integer.parseInt(request.getParameter("num"));
}catch(NumberFormatException ne){
out.println("非法输入");
return;
}
//获取随机数
int realnum=(Integer)session.getAttribute("count");
if(num>realnum){
out.println("再小一点");
session.setAttribute("times", times+1);
}else if(num<realnum){
out.println("再大一点");
session.setAttribute("times", times+1);
}else{
times=(Integer)session.getAttribute("times");
out.println("答对了,花了"+times+"次");
session.removeAttribute("times");
}
}
- 利用jsp/servlet/Ajax,实现一个猜数字游戏!
- 利用C++编写一个“猜数字”游戏
- 利用JSP实现Ajax
- 使用jsp和servlet写的一猜数字游戏
- 一个Ajax数字排列游戏
- Jsp servlet+jquery+ajax实现
- c语言实现一个猜数字游戏。
- 使用JSP技术实现百万富翁猜数字游戏
- 利用do{}while();循环语句实现人机猜数字游戏
- jsp编写猜数字游戏
- ajax+jsp+servlet+mysql实现登陆验证
- 猜数字游戏实现
- 实现猜数字游戏
- 猜数字游戏。编写一个Java应用程序,实现功能:
- 用python实现的一个猜数字游戏,
- JSP+JavaBean+Servlet实现一个留言板
- JSP+JavaBean+Servlet实现一个留言板
- 写一个猜数字游戏
- 解密module_init幕后的故事
- 部分面试题解题思路
- 正则表达式基本语法
- STL之函数对象事例
- 山西为全省低收入农户每户发放1吨取暖煤-陕西-低收入农户-煤
- 利用jsp/servlet/Ajax,实现一个猜数字游戏!
- HDOJ 1000
- 真正成功的theos环境配置
- #include <cctype>
- limiter Gem使用记录一
- 知道为什么需要htons(), ntohl(), ntohs(),htons() 函数吗? (转)
- depend工具监测程序
- 一个简单的Docbook 5.0例子
- 创建弹出菜单